2010-03-17 12:08:17 +01:00
|
|
|
/*
|
|
|
|
* vhost support
|
|
|
|
*
|
|
|
|
* Copyright Red Hat, Inc. 2010
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Michael S. Tsirkin <mst@redhat.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the COPYING file in the top-level directory.
|
2012-01-13 17:44:23 +01:00
|
|
|
*
|
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2010-03-17 12:08:17 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
2013-02-05 17:06:20 +01:00
|
|
|
#include "hw/virtio/vhost.h"
|
2010-03-17 12:08:17 +01:00
|
|
|
#include "hw/hw.h"
|
2013-05-13 13:29:47 +02:00
|
|
|
#include "qemu/atomic.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/range.h"
|
2010-04-08 22:49:50 +02:00
|
|
|
#include <linux/vhost.h>
|
2012-12-17 18:19:49 +01:00
|
|
|
#include "exec/address-spaces.h"
|
2013-04-24 10:21:21 +02:00
|
|
|
#include "hw/virtio/virtio-bus.h"
|
2010-03-17 12:08:17 +01:00
|
|
|
|
|
|
|
static void vhost_dev_sync_region(struct vhost_dev *dev,
|
2011-12-19 12:18:13 +01:00
|
|
|
MemoryRegionSection *section,
|
2010-03-17 12:08:17 +01:00
|
|
|
uint64_t mfirst, uint64_t mlast,
|
|
|
|
uint64_t rfirst, uint64_t rlast)
|
|
|
|
{
|
|
|
|
uint64_t start = MAX(mfirst, rfirst);
|
|
|
|
uint64_t end = MIN(mlast, rlast);
|
|
|
|
vhost_log_chunk_t *from = dev->log + start / VHOST_LOG_CHUNK;
|
|
|
|
vhost_log_chunk_t *to = dev->log + end / VHOST_LOG_CHUNK + 1;
|
|
|
|
uint64_t addr = (start / VHOST_LOG_CHUNK) * VHOST_LOG_CHUNK;
|
|
|
|
|
|
|
|
if (end < start) {
|
|
|
|
return;
|
|
|
|
}
|
2010-08-13 15:54:52 +02:00
|
|
|
assert(end / VHOST_LOG_CHUNK < dev->log_size);
|
2012-04-01 10:39:43 +02:00
|
|
|
assert(start / VHOST_LOG_CHUNK < dev->log_size);
|
2010-08-13 15:54:52 +02:00
|
|
|
|
2010-03-17 12:08:17 +01:00
|
|
|
for (;from < to; ++from) {
|
|
|
|
vhost_log_chunk_t log;
|
|
|
|
int bit;
|
|
|
|
/* We first check with non-atomic: much cheaper,
|
|
|
|
* and we expect non-dirty to be the common case. */
|
|
|
|
if (!*from) {
|
2010-11-27 15:05:07 +01:00
|
|
|
addr += VHOST_LOG_CHUNK;
|
2010-03-17 12:08:17 +01:00
|
|
|
continue;
|
|
|
|
}
|
2013-05-13 13:29:47 +02:00
|
|
|
/* Data must be read atomically. We don't really need barrier semantics
|
|
|
|
* but it's easier to use atomic_* than roll our own. */
|
|
|
|
log = atomic_xchg(from, 0);
|
2010-03-17 12:08:17 +01:00
|
|
|
while ((bit = sizeof(log) > sizeof(int) ?
|
|
|
|
ffsll(log) : ffs(log))) {
|
2013-02-21 12:16:06 +01:00
|
|
|
hwaddr page_addr;
|
|
|
|
hwaddr section_offset;
|
|
|
|
hwaddr mr_offset;
|
2010-03-17 12:08:17 +01:00
|
|
|
bit -= 1;
|
2013-02-21 12:16:06 +01:00
|
|
|
page_addr = addr + bit * VHOST_LOG_PAGE;
|
|
|
|
section_offset = page_addr - section->offset_within_address_space;
|
|
|
|
mr_offset = section_offset + section->offset_within_region;
|
|
|
|
memory_region_set_dirty(section->mr, mr_offset, VHOST_LOG_PAGE);
|
2010-03-17 12:08:17 +01:00
|
|
|
log &= ~(0x1ull << bit);
|
|
|
|
}
|
|
|
|
addr += VHOST_LOG_CHUNK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-18 13:06:05 +01:00
|
|
|
static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
|
2011-12-19 12:18:13 +01:00
|
|
|
MemoryRegionSection *section,
|
2013-02-21 12:16:06 +01:00
|
|
|
hwaddr first,
|
|
|
|
hwaddr last)
|
2010-03-17 12:08:17 +01:00
|
|
|
{
|
|
|
|
int i;
|
2013-02-21 12:16:06 +01:00
|
|
|
hwaddr start_addr;
|
|
|
|
hwaddr end_addr;
|
2011-12-18 13:06:05 +01:00
|
|
|
|
2010-03-17 12:08:17 +01:00
|
|
|
if (!dev->log_enabled || !dev->started) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-02-21 12:16:06 +01:00
|
|
|
start_addr = section->offset_within_address_space;
|
2013-05-27 10:08:27 +02:00
|
|
|
end_addr = range_get_last(start_addr, int128_get64(section->size));
|
2013-02-21 12:16:06 +01:00
|
|
|
start_addr = MAX(first, start_addr);
|
|
|
|
end_addr = MIN(last, end_addr);
|
|
|
|
|
2010-03-17 12:08:17 +01:00
|
|
|
for (i = 0; i < dev->mem->nregions; ++i) {
|
|
|
|
struct vhost_memory_region *reg = dev->mem->regions + i;
|
2011-12-19 12:18:13 +01:00
|
|
|
vhost_dev_sync_region(dev, section, start_addr, end_addr,
|
2010-03-17 12:08:17 +01:00
|
|
|
reg->guest_phys_addr,
|
|
|
|
range_get_last(reg->guest_phys_addr,
|
|
|
|
reg->memory_size));
|
|
|
|
}
|
|
|
|
for (i = 0; i < dev->nvqs; ++i) {
|
|
|
|
struct vhost_virtqueue *vq = dev->vqs + i;
|
2011-12-19 12:18:13 +01:00
|
|
|
vhost_dev_sync_region(dev, section, start_addr, end_addr, vq->used_phys,
|
2010-03-17 12:08:17 +01:00
|
|
|
range_get_last(vq->used_phys, vq->used_size));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-18 13:06:05 +01:00
|
|
|
static void vhost_log_sync(MemoryListener *listener,
|
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
|
|
|
struct vhost_dev *dev = container_of(listener, struct vhost_dev,
|
|
|
|
memory_listener);
|
2013-02-21 12:16:06 +01:00
|
|
|
vhost_sync_dirty_bitmap(dev, section, 0x0, ~0x0ULL);
|
|
|
|
}
|
2011-12-18 13:06:05 +01:00
|
|
|
|
2013-02-21 12:16:06 +01:00
|
|
|
static void vhost_log_sync_range(struct vhost_dev *dev,
|
|
|
|
hwaddr first, hwaddr last)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
/* FIXME: this is N^2 in number of sections */
|
|
|
|
for (i = 0; i < dev->n_mem_sections; ++i) {
|
|
|
|
MemoryRegionSection *section = &dev->mem_sections[i];
|
|
|
|
vhost_sync_dirty_bitmap(dev, section, first, last);
|
|
|
|
}
|
2011-12-18 13:06:05 +01:00
|
|
|
}
|
|
|
|
|
2010-03-17 12:08:17 +01:00
|
|
|
/* Assign/unassign. Keep an unsorted array of non-overlapping
|
|
|
|
* memory regions in dev->mem. */
|
|
|
|
static void vhost_dev_unassign_memory(struct vhost_dev *dev,
|
|
|
|
uint64_t start_addr,
|
|
|
|
uint64_t size)
|
|
|
|
{
|
|
|
|
int from, to, n = dev->mem->nregions;
|
|
|
|
/* Track overlapping/split regions for sanity checking. */
|
|
|
|
int overlap_start = 0, overlap_end = 0, overlap_middle = 0, split = 0;
|
|
|
|
|
|
|
|
for (from = 0, to = 0; from < n; ++from, ++to) {
|
|
|
|
struct vhost_memory_region *reg = dev->mem->regions + to;
|
|
|
|
uint64_t reglast;
|
|
|
|
uint64_t memlast;
|
|
|
|
uint64_t change;
|
|
|
|
|
|
|
|
/* clone old region */
|
|
|
|
if (to != from) {
|
|
|
|
memcpy(reg, dev->mem->regions + from, sizeof *reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No overlap is simple */
|
|
|
|
if (!ranges_overlap(reg->guest_phys_addr, reg->memory_size,
|
|
|
|
start_addr, size)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Split only happens if supplied region
|
|
|
|
* is in the middle of an existing one. Thus it can not
|
|
|
|
* overlap with any other existing region. */
|
|
|
|
assert(!split);
|
|
|
|
|
|
|
|
reglast = range_get_last(reg->guest_phys_addr, reg->memory_size);
|
|
|
|
memlast = range_get_last(start_addr, size);
|
|
|
|
|
|
|
|
/* Remove whole region */
|
|
|
|
if (start_addr <= reg->guest_phys_addr && memlast >= reglast) {
|
|
|
|
--dev->mem->nregions;
|
|
|
|
--to;
|
|
|
|
++overlap_middle;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Shrink region */
|
|
|
|
if (memlast >= reglast) {
|
|
|
|
reg->memory_size = start_addr - reg->guest_phys_addr;
|
|
|
|
assert(reg->memory_size);
|
|
|
|
assert(!overlap_end);
|
|
|
|
++overlap_end;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Shift region */
|
|
|
|
if (start_addr <= reg->guest_phys_addr) {
|
|
|
|
change = memlast + 1 - reg->guest_phys_addr;
|
|
|
|
reg->memory_size -= change;
|
|
|
|
reg->guest_phys_addr += change;
|
|
|
|
reg->userspace_addr += change;
|
|
|
|
assert(reg->memory_size);
|
|
|
|
assert(!overlap_start);
|
|
|
|
++overlap_start;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This only happens if supplied region
|
|
|
|
* is in the middle of an existing one. Thus it can not
|
|
|
|
* overlap with any other existing region. */
|
|
|
|
assert(!overlap_start);
|
|
|
|
assert(!overlap_end);
|
|
|
|
assert(!overlap_middle);
|
|
|
|
/* Split region: shrink first part, shift second part. */
|
|
|
|
memcpy(dev->mem->regions + n, reg, sizeof *reg);
|
|
|
|
reg->memory_size = start_addr - reg->guest_phys_addr;
|
|
|
|
assert(reg->memory_size);
|
|
|
|
change = memlast + 1 - reg->guest_phys_addr;
|
|
|
|
reg = dev->mem->regions + n;
|
|
|
|
reg->memory_size -= change;
|
|
|
|
assert(reg->memory_size);
|
|
|
|
reg->guest_phys_addr += change;
|
|
|
|
reg->userspace_addr += change;
|
|
|
|
/* Never add more than 1 region */
|
|
|
|
assert(dev->mem->nregions == n);
|
|
|
|
++dev->mem->nregions;
|
|
|
|
++split;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called after unassign, so no regions overlap the given range. */
|
|
|
|
static void vhost_dev_assign_memory(struct vhost_dev *dev,
|
|
|
|
uint64_t start_addr,
|
|
|
|
uint64_t size,
|
|
|
|
uint64_t uaddr)
|
|
|
|
{
|
|
|
|
int from, to;
|
|
|
|
struct vhost_memory_region *merged = NULL;
|
|
|
|
for (from = 0, to = 0; from < dev->mem->nregions; ++from, ++to) {
|
|
|
|
struct vhost_memory_region *reg = dev->mem->regions + to;
|
|
|
|
uint64_t prlast, urlast;
|
|
|
|
uint64_t pmlast, umlast;
|
|
|
|
uint64_t s, e, u;
|
|
|
|
|
|
|
|
/* clone old region */
|
|
|
|
if (to != from) {
|
|
|
|
memcpy(reg, dev->mem->regions + from, sizeof *reg);
|
|
|
|
}
|
|
|
|
prlast = range_get_last(reg->guest_phys_addr, reg->memory_size);
|
|
|
|
pmlast = range_get_last(start_addr, size);
|
|
|
|
urlast = range_get_last(reg->userspace_addr, reg->memory_size);
|
|
|
|
umlast = range_get_last(uaddr, size);
|
|
|
|
|
|
|
|
/* check for overlapping regions: should never happen. */
|
|
|
|
assert(prlast < start_addr || pmlast < reg->guest_phys_addr);
|
|
|
|
/* Not an adjacent or overlapping region - do not merge. */
|
|
|
|
if ((prlast + 1 != start_addr || urlast + 1 != uaddr) &&
|
|
|
|
(pmlast + 1 != reg->guest_phys_addr ||
|
|
|
|
umlast + 1 != reg->userspace_addr)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (merged) {
|
|
|
|
--to;
|
|
|
|
assert(to >= 0);
|
|
|
|
} else {
|
|
|
|
merged = reg;
|
|
|
|
}
|
|
|
|
u = MIN(uaddr, reg->userspace_addr);
|
|
|
|
s = MIN(start_addr, reg->guest_phys_addr);
|
|
|
|
e = MAX(pmlast, prlast);
|
|
|
|
uaddr = merged->userspace_addr = u;
|
|
|
|
start_addr = merged->guest_phys_addr = s;
|
|
|
|
size = merged->memory_size = e - s + 1;
|
|
|
|
assert(merged->memory_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!merged) {
|
|
|
|
struct vhost_memory_region *reg = dev->mem->regions + to;
|
|
|
|
memset(reg, 0, sizeof *reg);
|
|
|
|
reg->memory_size = size;
|
|
|
|
assert(reg->memory_size);
|
|
|
|
reg->guest_phys_addr = start_addr;
|
|
|
|
reg->userspace_addr = uaddr;
|
|
|
|
++to;
|
|
|
|
}
|
|
|
|
assert(to <= dev->mem->nregions + 1);
|
|
|
|
dev->mem->nregions = to;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t vhost_get_log_size(struct vhost_dev *dev)
|
|
|
|
{
|
|
|
|
uint64_t log_size = 0;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < dev->mem->nregions; ++i) {
|
|
|
|
struct vhost_memory_region *reg = dev->mem->regions + i;
|
|
|
|
uint64_t last = range_get_last(reg->guest_phys_addr,
|
|
|
|
reg->memory_size);
|
|
|
|
log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
|
|
|
|
}
|
|
|
|
for (i = 0; i < dev->nvqs; ++i) {
|
|
|
|
struct vhost_virtqueue *vq = dev->vqs + i;
|
|
|
|
uint64_t last = vq->used_phys + vq->used_size - 1;
|
|
|
|
log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
|
|
|
|
}
|
|
|
|
return log_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void vhost_dev_log_resize(struct vhost_dev* dev, uint64_t size)
|
|
|
|
{
|
|
|
|
vhost_log_chunk_t *log;
|
|
|
|
uint64_t log_base;
|
2013-02-21 12:16:06 +01:00
|
|
|
int r;
|
2013-01-22 11:07:56 +01:00
|
|
|
|
|
|
|
log = g_malloc0(size * sizeof *log);
|
2010-03-17 12:08:17 +01:00
|
|
|
log_base = (uint64_t)(unsigned long)log;
|
|
|
|
r = ioctl(dev->control, VHOST_SET_LOG_BASE, &log_base);
|
|
|
|
assert(r >= 0);
|
2013-02-21 12:16:06 +01:00
|
|
|
/* Sync only the range covered by the old log */
|
|
|
|
if (dev->log_size) {
|
|
|
|
vhost_log_sync_range(dev, 0, dev->log_size * VHOST_LOG_CHUNK - 1);
|
2011-12-19 12:18:13 +01:00
|
|
|
}
|
2010-03-17 12:08:17 +01:00
|
|
|
if (dev->log) {
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(dev->log);
|
2010-03-17 12:08:17 +01:00
|
|
|
}
|
|
|
|
dev->log = log;
|
|
|
|
dev->log_size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vhost_verify_ring_mappings(struct vhost_dev *dev,
|
|
|
|
uint64_t start_addr,
|
|
|
|
uint64_t size)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < dev->nvqs; ++i) {
|
|
|
|
struct vhost_virtqueue *vq = dev->vqs + i;
|
2012-10-23 12:30:10 +02:00
|
|
|
hwaddr l;
|
2010-03-17 12:08:17 +01:00
|
|
|
void *p;
|
|
|
|
|
|
|
|
if (!ranges_overlap(start_addr, size, vq->ring_phys, vq->ring_size)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
l = vq->ring_size;
|
|
|
|
p = cpu_physical_memory_map(vq->ring_phys, &l, 1);
|
|
|
|
if (!p || l != vq->ring_size) {
|
|
|
|
fprintf(stderr, "Unable to map ring buffer for ring %d\n", i);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
if (p != vq->ring) {
|
|
|
|
fprintf(stderr, "Ring buffer relocated for ring %d\n", i);
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
cpu_physical_memory_unmap(p, l, 0, 0);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-31 15:45:51 +02:00
|
|
|
static struct vhost_memory_region *vhost_dev_find_reg(struct vhost_dev *dev,
|
|
|
|
uint64_t start_addr,
|
|
|
|
uint64_t size)
|
|
|
|
{
|
|
|
|
int i, n = dev->mem->nregions;
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
struct vhost_memory_region *reg = dev->mem->regions + i;
|
|
|
|
if (ranges_overlap(reg->guest_phys_addr, reg->memory_size,
|
|
|
|
start_addr, size)) {
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool vhost_dev_cmp_memory(struct vhost_dev *dev,
|
|
|
|
uint64_t start_addr,
|
|
|
|
uint64_t size,
|
|
|
|
uint64_t uaddr)
|
|
|
|
{
|
|
|
|
struct vhost_memory_region *reg = vhost_dev_find_reg(dev, start_addr, size);
|
|
|
|
uint64_t reglast;
|
|
|
|
uint64_t memlast;
|
|
|
|
|
|
|
|
if (!reg) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
reglast = range_get_last(reg->guest_phys_addr, reg->memory_size);
|
|
|
|
memlast = range_get_last(start_addr, size);
|
|
|
|
|
|
|
|
/* Need to extend region? */
|
|
|
|
if (start_addr < reg->guest_phys_addr || memlast > reglast) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/* userspace_addr changed? */
|
|
|
|
return uaddr != reg->userspace_addr + start_addr - reg->guest_phys_addr;
|
|
|
|
}
|
|
|
|
|
2011-12-18 13:06:05 +01:00
|
|
|
static void vhost_set_memory(MemoryListener *listener,
|
|
|
|
MemoryRegionSection *section,
|
|
|
|
bool add)
|
2010-03-17 12:08:17 +01:00
|
|
|
{
|
2011-12-18 13:06:05 +01:00
|
|
|
struct vhost_dev *dev = container_of(listener, struct vhost_dev,
|
|
|
|
memory_listener);
|
2012-10-23 12:30:10 +02:00
|
|
|
hwaddr start_addr = section->offset_within_address_space;
|
2013-05-27 10:08:27 +02:00
|
|
|
ram_addr_t size = int128_get64(section->size);
|
2011-12-18 13:06:05 +01:00
|
|
|
bool log_dirty = memory_region_is_logging(section->mr);
|
2010-03-17 12:08:17 +01:00
|
|
|
int s = offsetof(struct vhost_memory, regions) +
|
|
|
|
(dev->mem->nregions + 1) * sizeof dev->mem->regions[0];
|
2011-12-18 13:06:05 +01:00
|
|
|
void *ram;
|
|
|
|
|
2011-08-21 05:09:37 +02:00
|
|
|
dev->mem = g_realloc(dev->mem, s);
|
2010-03-17 12:08:17 +01:00
|
|
|
|
2011-04-06 21:30:24 +02:00
|
|
|
if (log_dirty) {
|
2011-12-18 13:06:05 +01:00
|
|
|
add = false;
|
2011-04-06 21:30:24 +02:00
|
|
|
}
|
|
|
|
|
2010-03-17 12:08:17 +01:00
|
|
|
assert(size);
|
|
|
|
|
2011-03-31 15:45:51 +02:00
|
|
|
/* Optimize no-change case. At least cirrus_vga does this a lot at this time. */
|
2012-01-09 12:09:40 +01:00
|
|
|
ram = memory_region_get_ram_ptr(section->mr) + section->offset_within_region;
|
2011-12-18 13:06:05 +01:00
|
|
|
if (add) {
|
|
|
|
if (!vhost_dev_cmp_memory(dev, start_addr, size, (uintptr_t)ram)) {
|
2011-03-31 15:45:51 +02:00
|
|
|
/* Region exists with same address. Nothing to do. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!vhost_dev_find_reg(dev, start_addr, size)) {
|
|
|
|
/* Removing region that we don't access. Nothing to do. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-17 12:08:17 +01:00
|
|
|
vhost_dev_unassign_memory(dev, start_addr, size);
|
2011-12-18 13:06:05 +01:00
|
|
|
if (add) {
|
2010-03-17 12:08:17 +01:00
|
|
|
/* Add given mapping, merging adjacent regions if any */
|
2011-12-18 13:06:05 +01:00
|
|
|
vhost_dev_assign_memory(dev, start_addr, size, (uintptr_t)ram);
|
2010-03-17 12:08:17 +01:00
|
|
|
} else {
|
|
|
|
/* Remove old mapping for this memory, if any. */
|
|
|
|
vhost_dev_unassign_memory(dev, start_addr, size);
|
|
|
|
}
|
2013-04-03 11:15:11 +02:00
|
|
|
dev->mem_changed_start_addr = MIN(dev->mem_changed_start_addr, start_addr);
|
|
|
|
dev->mem_changed_end_addr = MAX(dev->mem_changed_end_addr, start_addr + size - 1);
|
|
|
|
dev->memory_changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool vhost_section(MemoryRegionSection *section)
|
|
|
|
{
|
|
|
|
return memory_region_is_ram(section->mr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vhost_begin(MemoryListener *listener)
|
|
|
|
{
|
|
|
|
struct vhost_dev *dev = container_of(listener, struct vhost_dev,
|
|
|
|
memory_listener);
|
|
|
|
dev->mem_changed_end_addr = 0;
|
|
|
|
dev->mem_changed_start_addr = -1;
|
|
|
|
}
|
2010-03-17 12:08:17 +01:00
|
|
|
|
2013-04-03 11:15:11 +02:00
|
|
|
static void vhost_commit(MemoryListener *listener)
|
|
|
|
{
|
|
|
|
struct vhost_dev *dev = container_of(listener, struct vhost_dev,
|
|
|
|
memory_listener);
|
|
|
|
hwaddr start_addr = 0;
|
|
|
|
ram_addr_t size = 0;
|
|
|
|
uint64_t log_size;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (!dev->memory_changed) {
|
|
|
|
return;
|
|
|
|
}
|
2010-03-17 12:08:17 +01:00
|
|
|
if (!dev->started) {
|
|
|
|
return;
|
|
|
|
}
|
2013-04-03 11:15:11 +02:00
|
|
|
if (dev->mem_changed_start_addr > dev->mem_changed_end_addr) {
|
|
|
|
return;
|
|
|
|
}
|
2010-03-17 12:08:17 +01:00
|
|
|
|
|
|
|
if (dev->started) {
|
2013-04-03 11:15:11 +02:00
|
|
|
start_addr = dev->mem_changed_start_addr;
|
|
|
|
size = dev->mem_changed_end_addr - dev->mem_changed_start_addr + 1;
|
|
|
|
|
2010-03-17 12:08:17 +01:00
|
|
|
r = vhost_verify_ring_mappings(dev, start_addr, size);
|
|
|
|
assert(r >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dev->log_enabled) {
|
|
|
|
r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
|
|
|
|
assert(r >= 0);
|
2013-04-03 11:15:11 +02:00
|
|
|
dev->memory_changed = false;
|
2010-03-17 12:08:17 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
log_size = vhost_get_log_size(dev);
|
|
|
|
/* We allocate an extra 4K bytes to log,
|
|
|
|
* to reduce the * number of reallocations. */
|
|
|
|
#define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
|
|
|
|
/* To log more, must increase log size before table update. */
|
|
|
|
if (dev->log_size < log_size) {
|
|
|
|
vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER);
|
|
|
|
}
|
|
|
|
r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
|
|
|
|
assert(r >= 0);
|
|
|
|
/* To log less, can only decrease log size after table update. */
|
|
|
|
if (dev->log_size > log_size + VHOST_LOG_BUFFER) {
|
|
|
|
vhost_dev_log_resize(dev, log_size);
|
|
|
|
}
|
2013-04-03 11:15:11 +02:00
|
|
|
dev->memory_changed = false;
|
2012-02-08 20:36:02 +01:00
|
|
|
}
|
|
|
|
|
2011-12-18 13:06:05 +01:00
|
|
|
static void vhost_region_add(MemoryListener *listener,
|
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
2011-12-19 12:18:13 +01:00
|
|
|
struct vhost_dev *dev = container_of(listener, struct vhost_dev,
|
|
|
|
memory_listener);
|
|
|
|
|
2012-01-09 13:01:39 +01:00
|
|
|
if (!vhost_section(section)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-19 12:18:13 +01:00
|
|
|
++dev->n_mem_sections;
|
|
|
|
dev->mem_sections = g_renew(MemoryRegionSection, dev->mem_sections,
|
|
|
|
dev->n_mem_sections);
|
|
|
|
dev->mem_sections[dev->n_mem_sections - 1] = *section;
|
2013-05-06 10:46:11 +02:00
|
|
|
memory_region_ref(section->mr);
|
2011-12-18 13:06:05 +01:00
|
|
|
vhost_set_memory(listener, section, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vhost_region_del(MemoryListener *listener,
|
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
2011-12-19 12:18:13 +01:00
|
|
|
struct vhost_dev *dev = container_of(listener, struct vhost_dev,
|
|
|
|
memory_listener);
|
|
|
|
int i;
|
|
|
|
|
2012-01-09 13:01:39 +01:00
|
|
|
if (!vhost_section(section)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-18 13:06:05 +01:00
|
|
|
vhost_set_memory(listener, section, false);
|
2013-05-06 10:46:11 +02:00
|
|
|
memory_region_unref(section->mr);
|
2011-12-19 12:18:13 +01:00
|
|
|
for (i = 0; i < dev->n_mem_sections; ++i) {
|
|
|
|
if (dev->mem_sections[i].offset_within_address_space
|
|
|
|
== section->offset_within_address_space) {
|
|
|
|
--dev->n_mem_sections;
|
|
|
|
memmove(&dev->mem_sections[i], &dev->mem_sections[i+1],
|
2012-01-09 12:59:50 +01:00
|
|
|
(dev->n_mem_sections - i) * sizeof(*dev->mem_sections));
|
2011-12-19 12:18:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-12-18 13:06:05 +01:00
|
|
|
}
|
|
|
|
|
2012-02-08 20:36:02 +01:00
|
|
|
static void vhost_region_nop(MemoryListener *listener,
|
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-03-17 12:08:17 +01:00
|
|
|
static int vhost_virtqueue_set_addr(struct vhost_dev *dev,
|
|
|
|
struct vhost_virtqueue *vq,
|
|
|
|
unsigned idx, bool enable_log)
|
|
|
|
{
|
|
|
|
struct vhost_vring_addr addr = {
|
|
|
|
.index = idx,
|
2010-04-01 23:59:51 +02:00
|
|
|
.desc_user_addr = (uint64_t)(unsigned long)vq->desc,
|
|
|
|
.avail_user_addr = (uint64_t)(unsigned long)vq->avail,
|
|
|
|
.used_user_addr = (uint64_t)(unsigned long)vq->used,
|
2010-03-17 12:08:17 +01:00
|
|
|
.log_guest_addr = vq->used_phys,
|
|
|
|
.flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0,
|
|
|
|
};
|
|
|
|
int r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
|
|
|
|
if (r < 0) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vhost_dev_set_features(struct vhost_dev *dev, bool enable_log)
|
|
|
|
{
|
|
|
|
uint64_t features = dev->acked_features;
|
|
|
|
int r;
|
|
|
|
if (enable_log) {
|
|
|
|
features |= 0x1 << VHOST_F_LOG_ALL;
|
|
|
|
}
|
|
|
|
r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
|
|
|
|
return r < 0 ? -errno : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vhost_dev_set_log(struct vhost_dev *dev, bool enable_log)
|
|
|
|
{
|
|
|
|
int r, t, i;
|
|
|
|
r = vhost_dev_set_features(dev, enable_log);
|
|
|
|
if (r < 0) {
|
|
|
|
goto err_features;
|
|
|
|
}
|
|
|
|
for (i = 0; i < dev->nvqs; ++i) {
|
|
|
|
r = vhost_virtqueue_set_addr(dev, dev->vqs + i, i,
|
|
|
|
enable_log);
|
|
|
|
if (r < 0) {
|
|
|
|
goto err_vq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
err_vq:
|
|
|
|
for (; i >= 0; --i) {
|
|
|
|
t = vhost_virtqueue_set_addr(dev, dev->vqs + i, i,
|
|
|
|
dev->log_enabled);
|
|
|
|
assert(t >= 0);
|
|
|
|
}
|
|
|
|
t = vhost_dev_set_features(dev, dev->log_enabled);
|
|
|
|
assert(t >= 0);
|
|
|
|
err_features:
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-12-18 13:06:05 +01:00
|
|
|
static int vhost_migration_log(MemoryListener *listener, int enable)
|
2010-03-17 12:08:17 +01:00
|
|
|
{
|
2011-12-18 13:06:05 +01:00
|
|
|
struct vhost_dev *dev = container_of(listener, struct vhost_dev,
|
|
|
|
memory_listener);
|
2010-03-17 12:08:17 +01:00
|
|
|
int r;
|
|
|
|
if (!!enable == dev->log_enabled) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!dev->started) {
|
|
|
|
dev->log_enabled = enable;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!enable) {
|
|
|
|
r = vhost_dev_set_log(dev, false);
|
|
|
|
if (r < 0) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (dev->log) {
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(dev->log);
|
2010-03-17 12:08:17 +01:00
|
|
|
}
|
|
|
|
dev->log = NULL;
|
|
|
|
dev->log_size = 0;
|
|
|
|
} else {
|
|
|
|
vhost_dev_log_resize(dev, vhost_get_log_size(dev));
|
|
|
|
r = vhost_dev_set_log(dev, true);
|
|
|
|
if (r < 0) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dev->log_enabled = enable;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-18 13:06:05 +01:00
|
|
|
static void vhost_log_global_start(MemoryListener *listener)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = vhost_migration_log(listener, true);
|
|
|
|
if (r < 0) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vhost_log_global_stop(MemoryListener *listener)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = vhost_migration_log(listener, false);
|
|
|
|
if (r < 0) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vhost_log_start(MemoryListener *listener,
|
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
|
|
|
/* FIXME: implement */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vhost_log_stop(MemoryListener *listener,
|
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
|
|
|
/* FIXME: implement */
|
|
|
|
}
|
|
|
|
|
2012-12-24 16:37:01 +01:00
|
|
|
static int vhost_virtqueue_start(struct vhost_dev *dev,
|
2010-03-17 12:08:17 +01:00
|
|
|
struct VirtIODevice *vdev,
|
|
|
|
struct vhost_virtqueue *vq,
|
|
|
|
unsigned idx)
|
|
|
|
{
|
2012-10-23 12:30:10 +02:00
|
|
|
hwaddr s, l, a;
|
2010-03-17 12:08:17 +01:00
|
|
|
int r;
|
2013-01-30 12:12:35 +01:00
|
|
|
int vhost_vq_index = idx - dev->vq_index;
|
2010-03-17 12:08:17 +01:00
|
|
|
struct vhost_vring_file file = {
|
2013-01-30 12:12:35 +01:00
|
|
|
.index = vhost_vq_index
|
2010-03-17 12:08:17 +01:00
|
|
|
};
|
|
|
|
struct vhost_vring_state state = {
|
2013-01-30 12:12:35 +01:00
|
|
|
.index = vhost_vq_index
|
2010-03-17 12:08:17 +01:00
|
|
|
};
|
|
|
|
struct VirtQueue *vvq = virtio_get_queue(vdev, idx);
|
|
|
|
|
2013-01-30 12:12:35 +01:00
|
|
|
assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
|
|
|
|
|
2010-03-17 12:08:17 +01:00
|
|
|
vq->num = state.num = virtio_queue_get_num(vdev, idx);
|
|
|
|
r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
|
|
|
|
if (r) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
state.num = virtio_queue_get_last_avail_idx(vdev, idx);
|
|
|
|
r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
|
|
|
|
if (r) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = l = virtio_queue_get_desc_size(vdev, idx);
|
|
|
|
a = virtio_queue_get_desc_addr(vdev, idx);
|
|
|
|
vq->desc = cpu_physical_memory_map(a, &l, 0);
|
|
|
|
if (!vq->desc || l != s) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto fail_alloc_desc;
|
|
|
|
}
|
|
|
|
s = l = virtio_queue_get_avail_size(vdev, idx);
|
|
|
|
a = virtio_queue_get_avail_addr(vdev, idx);
|
|
|
|
vq->avail = cpu_physical_memory_map(a, &l, 0);
|
|
|
|
if (!vq->avail || l != s) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto fail_alloc_avail;
|
|
|
|
}
|
|
|
|
vq->used_size = s = l = virtio_queue_get_used_size(vdev, idx);
|
|
|
|
vq->used_phys = a = virtio_queue_get_used_addr(vdev, idx);
|
|
|
|
vq->used = cpu_physical_memory_map(a, &l, 1);
|
|
|
|
if (!vq->used || l != s) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto fail_alloc_used;
|
|
|
|
}
|
|
|
|
|
|
|
|
vq->ring_size = s = l = virtio_queue_get_ring_size(vdev, idx);
|
|
|
|
vq->ring_phys = a = virtio_queue_get_ring_addr(vdev, idx);
|
|
|
|
vq->ring = cpu_physical_memory_map(a, &l, 1);
|
|
|
|
if (!vq->ring || l != s) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto fail_alloc_ring;
|
|
|
|
}
|
|
|
|
|
2013-01-30 12:12:35 +01:00
|
|
|
r = vhost_virtqueue_set_addr(dev, vq, vhost_vq_index, dev->log_enabled);
|
2010-03-17 12:08:17 +01:00
|
|
|
if (r < 0) {
|
|
|
|
r = -errno;
|
|
|
|
goto fail_alloc;
|
|
|
|
}
|
2013-01-30 12:12:35 +01:00
|
|
|
|
2010-03-17 12:08:17 +01:00
|
|
|
file.fd = event_notifier_get_fd(virtio_queue_get_host_notifier(vvq));
|
|
|
|
r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
|
|
|
|
if (r) {
|
2010-10-06 15:20:28 +02:00
|
|
|
r = -errno;
|
2010-03-17 12:08:17 +01:00
|
|
|
goto fail_kick;
|
|
|
|
}
|
|
|
|
|
2012-12-24 16:37:01 +01:00
|
|
|
/* Clear and discard previous events if any. */
|
|
|
|
event_notifier_test_and_clear(&vq->masked_notifier);
|
2010-03-17 12:08:17 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail_kick:
|
|
|
|
fail_alloc:
|
|
|
|
cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
|
|
|
|
0, 0);
|
|
|
|
fail_alloc_ring:
|
|
|
|
cpu_physical_memory_unmap(vq->used, virtio_queue_get_used_size(vdev, idx),
|
|
|
|
0, 0);
|
|
|
|
fail_alloc_used:
|
|
|
|
cpu_physical_memory_unmap(vq->avail, virtio_queue_get_avail_size(vdev, idx),
|
|
|
|
0, 0);
|
|
|
|
fail_alloc_avail:
|
|
|
|
cpu_physical_memory_unmap(vq->desc, virtio_queue_get_desc_size(vdev, idx),
|
|
|
|
0, 0);
|
|
|
|
fail_alloc_desc:
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-12-24 16:37:01 +01:00
|
|
|
static void vhost_virtqueue_stop(struct vhost_dev *dev,
|
2010-03-17 12:08:17 +01:00
|
|
|
struct VirtIODevice *vdev,
|
|
|
|
struct vhost_virtqueue *vq,
|
|
|
|
unsigned idx)
|
|
|
|
{
|
|
|
|
struct vhost_vring_state state = {
|
2013-01-30 12:12:35 +01:00
|
|
|
.index = idx - dev->vq_index
|
2010-03-17 12:08:17 +01:00
|
|
|
};
|
|
|
|
int r;
|
2013-01-30 12:12:35 +01:00
|
|
|
assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
|
2010-03-17 12:08:17 +01:00
|
|
|
r = ioctl(dev->control, VHOST_GET_VRING_BASE, &state);
|
|
|
|
if (r < 0) {
|
|
|
|
fprintf(stderr, "vhost VQ %d ring restore failed: %d\n", idx, r);
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
virtio_queue_set_last_avail_idx(vdev, idx, state.num);
|
2013-08-12 11:21:36 +02:00
|
|
|
virtio_queue_invalidate_signalled_used(vdev, idx);
|
2010-03-17 12:08:17 +01:00
|
|
|
assert (r >= 0);
|
|
|
|
cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
|
|
|
|
0, virtio_queue_get_ring_size(vdev, idx));
|
|
|
|
cpu_physical_memory_unmap(vq->used, virtio_queue_get_used_size(vdev, idx),
|
|
|
|
1, virtio_queue_get_used_size(vdev, idx));
|
|
|
|
cpu_physical_memory_unmap(vq->avail, virtio_queue_get_avail_size(vdev, idx),
|
|
|
|
0, virtio_queue_get_avail_size(vdev, idx));
|
|
|
|
cpu_physical_memory_unmap(vq->desc, virtio_queue_get_desc_size(vdev, idx),
|
|
|
|
0, virtio_queue_get_desc_size(vdev, idx));
|
|
|
|
}
|
|
|
|
|
2012-02-08 15:39:06 +01:00
|
|
|
static void vhost_eventfd_add(MemoryListener *listener,
|
|
|
|
MemoryRegionSection *section,
|
2012-07-05 17:16:27 +02:00
|
|
|
bool match_data, uint64_t data, EventNotifier *e)
|
2012-02-08 15:39:06 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vhost_eventfd_del(MemoryListener *listener,
|
|
|
|
MemoryRegionSection *section,
|
2012-07-05 17:16:27 +02:00
|
|
|
bool match_data, uint64_t data, EventNotifier *e)
|
2012-02-08 15:39:06 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-12-24 16:37:01 +01:00
|
|
|
static int vhost_virtqueue_init(struct vhost_dev *dev,
|
|
|
|
struct vhost_virtqueue *vq, int n)
|
|
|
|
{
|
|
|
|
struct vhost_vring_file file = {
|
|
|
|
.index = n,
|
|
|
|
};
|
|
|
|
int r = event_notifier_init(&vq->masked_notifier, 0);
|
|
|
|
if (r < 0) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
file.fd = event_notifier_get_fd(&vq->masked_notifier);
|
|
|
|
r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
|
|
|
|
if (r) {
|
|
|
|
r = -errno;
|
|
|
|
goto fail_call;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
fail_call:
|
|
|
|
event_notifier_cleanup(&vq->masked_notifier);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vhost_virtqueue_cleanup(struct vhost_virtqueue *vq)
|
|
|
|
{
|
|
|
|
event_notifier_cleanup(&vq->masked_notifier);
|
|
|
|
}
|
|
|
|
|
2012-08-21 22:52:08 +02:00
|
|
|
int vhost_dev_init(struct vhost_dev *hdev, int devfd, const char *devpath,
|
|
|
|
bool force)
|
2010-03-17 12:08:17 +01:00
|
|
|
{
|
|
|
|
uint64_t features;
|
2012-12-24 16:37:01 +01:00
|
|
|
int i, r;
|
2010-03-17 12:08:17 +01:00
|
|
|
if (devfd >= 0) {
|
|
|
|
hdev->control = devfd;
|
|
|
|
} else {
|
2012-08-21 22:52:08 +02:00
|
|
|
hdev->control = open(devpath, O_RDWR);
|
2010-03-17 12:08:17 +01:00
|
|
|
if (hdev->control < 0) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r = ioctl(hdev->control, VHOST_SET_OWNER, NULL);
|
|
|
|
if (r < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = ioctl(hdev->control, VHOST_GET_FEATURES, &features);
|
|
|
|
if (r < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-12-24 16:37:01 +01:00
|
|
|
|
|
|
|
for (i = 0; i < hdev->nvqs; ++i) {
|
|
|
|
r = vhost_virtqueue_init(hdev, hdev->vqs + i, i);
|
|
|
|
if (r < 0) {
|
|
|
|
goto fail_vq;
|
|
|
|
}
|
|
|
|
}
|
2010-03-17 12:08:17 +01:00
|
|
|
hdev->features = features;
|
|
|
|
|
2011-12-18 13:06:05 +01:00
|
|
|
hdev->memory_listener = (MemoryListener) {
|
2012-02-08 20:36:02 +01:00
|
|
|
.begin = vhost_begin,
|
|
|
|
.commit = vhost_commit,
|
2011-12-18 13:06:05 +01:00
|
|
|
.region_add = vhost_region_add,
|
|
|
|
.region_del = vhost_region_del,
|
2012-02-08 20:36:02 +01:00
|
|
|
.region_nop = vhost_region_nop,
|
2011-12-18 13:06:05 +01:00
|
|
|
.log_start = vhost_log_start,
|
|
|
|
.log_stop = vhost_log_stop,
|
|
|
|
.log_sync = vhost_log_sync,
|
|
|
|
.log_global_start = vhost_log_global_start,
|
|
|
|
.log_global_stop = vhost_log_global_stop,
|
2012-02-08 15:39:06 +01:00
|
|
|
.eventfd_add = vhost_eventfd_add,
|
|
|
|
.eventfd_del = vhost_eventfd_del,
|
2012-02-08 14:05:50 +01:00
|
|
|
.priority = 10
|
2011-12-18 13:06:05 +01:00
|
|
|
};
|
2011-08-21 05:09:37 +02:00
|
|
|
hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions));
|
2011-12-19 12:18:13 +01:00
|
|
|
hdev->n_mem_sections = 0;
|
|
|
|
hdev->mem_sections = NULL;
|
2010-03-17 12:08:17 +01:00
|
|
|
hdev->log = NULL;
|
|
|
|
hdev->log_size = 0;
|
|
|
|
hdev->log_enabled = false;
|
|
|
|
hdev->started = false;
|
2013-04-03 11:15:11 +02:00
|
|
|
hdev->memory_changed = false;
|
2012-10-02 20:13:51 +02:00
|
|
|
memory_listener_register(&hdev->memory_listener, &address_space_memory);
|
2011-02-01 21:13:42 +01:00
|
|
|
hdev->force = force;
|
2010-03-17 12:08:17 +01:00
|
|
|
return 0;
|
2012-12-24 16:37:01 +01:00
|
|
|
fail_vq:
|
|
|
|
while (--i >= 0) {
|
|
|
|
vhost_virtqueue_cleanup(hdev->vqs + i);
|
|
|
|
}
|
2010-03-17 12:08:17 +01:00
|
|
|
fail:
|
|
|
|
r = -errno;
|
|
|
|
close(hdev->control);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vhost_dev_cleanup(struct vhost_dev *hdev)
|
|
|
|
{
|
2012-12-24 16:37:01 +01:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < hdev->nvqs; ++i) {
|
|
|
|
vhost_virtqueue_cleanup(hdev->vqs + i);
|
|
|
|
}
|
2011-12-18 13:06:05 +01:00
|
|
|
memory_listener_unregister(&hdev->memory_listener);
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(hdev->mem);
|
2011-12-19 12:18:13 +01:00
|
|
|
g_free(hdev->mem_sections);
|
2010-03-17 12:08:17 +01:00
|
|
|
close(hdev->control);
|
|
|
|
}
|
|
|
|
|
2011-02-01 21:13:42 +01:00
|
|
|
bool vhost_dev_query(struct vhost_dev *hdev, VirtIODevice *vdev)
|
|
|
|
{
|
2013-04-24 10:21:21 +02:00
|
|
|
BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
|
|
|
|
VirtioBusState *vbus = VIRTIO_BUS(qbus);
|
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
|
|
|
|
|
|
|
|
return !k->query_guest_notifiers ||
|
|
|
|
k->query_guest_notifiers(qbus->parent) ||
|
|
|
|
hdev->force;
|
2011-02-01 21:13:42 +01:00
|
|
|
}
|
|
|
|
|
2011-08-11 09:21:18 +02:00
|
|
|
/* Stop processing guest IO notifications in qemu.
|
|
|
|
* Start processing them in vhost in kernel.
|
|
|
|
*/
|
|
|
|
int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
|
|
|
|
{
|
2013-04-24 10:21:21 +02:00
|
|
|
BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
|
|
|
|
VirtioBusState *vbus = VIRTIO_BUS(qbus);
|
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
|
2011-08-11 09:21:18 +02:00
|
|
|
int i, r;
|
2013-04-24 10:21:21 +02:00
|
|
|
if (!k->set_host_notifier) {
|
2011-08-11 09:21:18 +02:00
|
|
|
fprintf(stderr, "binding does not support host notifiers\n");
|
|
|
|
r = -ENOSYS;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < hdev->nvqs; ++i) {
|
2013-04-24 10:21:21 +02:00
|
|
|
r = k->set_host_notifier(qbus->parent, hdev->vq_index + i, true);
|
2011-08-11 09:21:18 +02:00
|
|
|
if (r < 0) {
|
|
|
|
fprintf(stderr, "vhost VQ %d notifier binding failed: %d\n", i, -r);
|
|
|
|
goto fail_vq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
fail_vq:
|
|
|
|
while (--i >= 0) {
|
2013-04-24 10:21:21 +02:00
|
|
|
r = k->set_host_notifier(qbus->parent, hdev->vq_index + i, false);
|
2011-08-11 09:21:18 +02:00
|
|
|
if (r < 0) {
|
|
|
|
fprintf(stderr, "vhost VQ %d notifier cleanup error: %d\n", i, -r);
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
assert (r >= 0);
|
|
|
|
}
|
|
|
|
fail:
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stop processing guest IO notifications in vhost.
|
|
|
|
* Start processing them in qemu.
|
|
|
|
* This might actually run the qemu handlers right away,
|
|
|
|
* so virtio in qemu must be completely setup when this is called.
|
|
|
|
*/
|
|
|
|
void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
|
|
|
|
{
|
2013-04-24 10:21:21 +02:00
|
|
|
BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
|
|
|
|
VirtioBusState *vbus = VIRTIO_BUS(qbus);
|
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
|
2011-08-11 09:21:18 +02:00
|
|
|
int i, r;
|
|
|
|
|
|
|
|
for (i = 0; i < hdev->nvqs; ++i) {
|
2013-04-24 10:21:21 +02:00
|
|
|
r = k->set_host_notifier(qbus->parent, hdev->vq_index + i, false);
|
2011-08-11 09:21:18 +02:00
|
|
|
if (r < 0) {
|
|
|
|
fprintf(stderr, "vhost VQ %d notifier cleanup failed: %d\n", i, -r);
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
assert (r >= 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-24 16:37:01 +01:00
|
|
|
/* Test and clear event pending status.
|
|
|
|
* Should be called after unmask to avoid losing events.
|
|
|
|
*/
|
|
|
|
bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n)
|
|
|
|
{
|
2013-01-30 12:12:35 +01:00
|
|
|
struct vhost_virtqueue *vq = hdev->vqs + n - hdev->vq_index;
|
2012-12-24 16:37:01 +01:00
|
|
|
assert(hdev->started);
|
2013-01-30 12:12:35 +01:00
|
|
|
assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs);
|
2012-12-24 16:37:01 +01:00
|
|
|
return event_notifier_test_and_clear(&vq->masked_notifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mask/unmask events from this vq. */
|
|
|
|
void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
|
|
|
|
bool mask)
|
|
|
|
{
|
|
|
|
struct VirtQueue *vvq = virtio_get_queue(vdev, n);
|
2013-01-30 12:12:35 +01:00
|
|
|
int r, index = n - hdev->vq_index;
|
2012-12-24 16:37:01 +01:00
|
|
|
|
|
|
|
assert(hdev->started);
|
2013-01-30 12:12:35 +01:00
|
|
|
assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs);
|
2012-12-24 16:37:01 +01:00
|
|
|
|
|
|
|
struct vhost_vring_file file = {
|
2013-01-30 12:12:35 +01:00
|
|
|
.index = index
|
2012-12-24 16:37:01 +01:00
|
|
|
};
|
|
|
|
if (mask) {
|
2013-01-30 12:12:35 +01:00
|
|
|
file.fd = event_notifier_get_fd(&hdev->vqs[index].masked_notifier);
|
2012-12-24 16:37:01 +01:00
|
|
|
} else {
|
|
|
|
file.fd = event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq));
|
|
|
|
}
|
|
|
|
r = ioctl(hdev->control, VHOST_SET_VRING_CALL, &file);
|
|
|
|
assert(r >= 0);
|
|
|
|
}
|
|
|
|
|
2011-08-11 09:21:18 +02:00
|
|
|
/* Host notifiers must be enabled at this point. */
|
2010-03-17 12:08:17 +01:00
|
|
|
int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
|
|
|
|
{
|
|
|
|
int i, r;
|
2012-12-25 16:41:07 +01:00
|
|
|
|
|
|
|
hdev->started = true;
|
|
|
|
|
2010-03-17 12:08:17 +01:00
|
|
|
r = vhost_dev_set_features(hdev, hdev->log_enabled);
|
|
|
|
if (r < 0) {
|
2010-10-06 15:20:17 +02:00
|
|
|
goto fail_features;
|
2010-03-17 12:08:17 +01:00
|
|
|
}
|
|
|
|
r = ioctl(hdev->control, VHOST_SET_MEM_TABLE, hdev->mem);
|
|
|
|
if (r < 0) {
|
|
|
|
r = -errno;
|
2010-10-06 15:20:17 +02:00
|
|
|
goto fail_mem;
|
2010-03-17 12:08:17 +01:00
|
|
|
}
|
2010-07-16 16:11:46 +02:00
|
|
|
for (i = 0; i < hdev->nvqs; ++i) {
|
2012-12-24 16:37:01 +01:00
|
|
|
r = vhost_virtqueue_start(hdev,
|
2013-01-30 12:12:35 +01:00
|
|
|
vdev,
|
|
|
|
hdev->vqs + i,
|
|
|
|
hdev->vq_index + i);
|
2010-07-16 16:11:46 +02:00
|
|
|
if (r < 0) {
|
|
|
|
goto fail_vq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-17 12:08:17 +01:00
|
|
|
if (hdev->log_enabled) {
|
|
|
|
hdev->log_size = vhost_get_log_size(hdev);
|
|
|
|
hdev->log = hdev->log_size ?
|
2011-08-21 05:09:37 +02:00
|
|
|
g_malloc0(hdev->log_size * sizeof *hdev->log) : NULL;
|
2010-03-17 12:08:17 +01:00
|
|
|
r = ioctl(hdev->control, VHOST_SET_LOG_BASE,
|
|
|
|
(uint64_t)(unsigned long)hdev->log);
|
|
|
|
if (r < 0) {
|
|
|
|
r = -errno;
|
2010-10-06 15:20:17 +02:00
|
|
|
goto fail_log;
|
2010-03-17 12:08:17 +01:00
|
|
|
}
|
|
|
|
}
|
2010-07-16 16:11:46 +02:00
|
|
|
|
2010-03-17 12:08:17 +01:00
|
|
|
return 0;
|
2010-10-06 15:20:17 +02:00
|
|
|
fail_log:
|
2010-03-17 12:08:17 +01:00
|
|
|
fail_vq:
|
|
|
|
while (--i >= 0) {
|
2012-12-24 16:37:01 +01:00
|
|
|
vhost_virtqueue_stop(hdev,
|
2013-01-30 12:12:35 +01:00
|
|
|
vdev,
|
|
|
|
hdev->vqs + i,
|
|
|
|
hdev->vq_index + i);
|
2010-03-17 12:08:17 +01:00
|
|
|
}
|
2013-01-30 12:12:35 +01:00
|
|
|
i = hdev->nvqs;
|
2010-10-06 15:20:17 +02:00
|
|
|
fail_mem:
|
|
|
|
fail_features:
|
2012-12-25 16:41:07 +01:00
|
|
|
|
|
|
|
hdev->started = false;
|
2010-03-17 12:08:17 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-08-11 09:21:18 +02:00
|
|
|
/* Host notifiers must be enabled at this point. */
|
2010-03-17 12:08:17 +01:00
|
|
|
void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
|
|
|
|
{
|
2013-01-30 12:12:35 +01:00
|
|
|
int i;
|
2010-10-06 15:20:17 +02:00
|
|
|
|
2010-03-17 12:08:17 +01:00
|
|
|
for (i = 0; i < hdev->nvqs; ++i) {
|
2012-12-24 16:37:01 +01:00
|
|
|
vhost_virtqueue_stop(hdev,
|
2013-01-30 12:12:35 +01:00
|
|
|
vdev,
|
|
|
|
hdev->vqs + i,
|
|
|
|
hdev->vq_index + i);
|
2010-03-17 12:08:17 +01:00
|
|
|
}
|
2013-02-21 12:16:06 +01:00
|
|
|
vhost_log_sync_range(hdev, 0, ~0x0ull);
|
2010-10-06 15:20:17 +02:00
|
|
|
|
2010-03-17 12:08:17 +01:00
|
|
|
hdev->started = false;
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(hdev->log);
|
2011-06-21 19:34:17 +02:00
|
|
|
hdev->log = NULL;
|
2010-03-17 12:08:17 +01:00
|
|
|
hdev->log_size = 0;
|
|
|
|
}
|
2013-01-30 12:12:35 +01:00
|
|
|
|