本文主要是介绍QEMU源码全解析 —— virtio(15),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
接前一篇文章:
上一回讲解了virtio_pci_device_plugged函数的前两部分,本回继续讲解virtio_pci_device_plugged函数的其余部分。为了便于理解,再次贴出virtio_pci_device_plugged函数源码,在hw/virtio/virtio-pci.c中,如下:
/* This is called by virtio-bus just after the device is plugged. */
static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
{VirtIOPCIProxy *proxy = VIRTIO_PCI(d);VirtioBusState *bus = &proxy->bus;bool legacy = virtio_pci_legacy(proxy);bool modern;bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;uint8_t *config;uint32_t size;VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);/** Virtio capabilities present without* VIRTIO_F_VERSION_1 confuses guests*/if (!proxy->ignore_backend_features &&!virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) {virtio_pci_disable_modern(proxy);if (!legacy) {error_setg(errp, "Device doesn't support modern mode, and legacy"" mode is disabled");error_append_hint(errp, "Set disable-legacy to off\n");return;}}modern = virtio_pci_modern(proxy);config = proxy->pci_dev.config;if (proxy->class_code) {pci_config_set_class(config, proxy->class_code);}if (legacy) {if (!virtio_legacy_allowed(vdev)) {/** To avoid migration issues, we allow legacy mode when legacy* check is disabled in the old machine types (< 5.1).*/if (virtio_legacy_check_disabled(vdev)) {warn_report("device is modern-only, but for backward ""compatibility legacy is allowed");} else {error_setg(errp,"device is modern-only, use disable-legacy=on");return;}}if (virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) {error_setg(errp, "VIRTIO_F_IOMMU_PLATFORM was supported by"" neither legacy nor transitional device");return;}/** Legacy and transitional devices use specific subsystem IDs.* Note that the subsystem vendor ID (config + PCI_SUBSYSTEM_VENDOR_ID)* is set to PCI_SUBVENDOR_ID_REDHAT_QUMRANET by default.*/pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));if (proxy->trans_devid) {pci_config_set_device_id(config, proxy->trans_devid);}} else {/* pure virtio-1.0 */pci_set_word(config + PCI_VENDOR_ID,PCI_VENDOR_ID_REDHAT_QUMRANET);pci_set_word(config + PCI_DEVICE_ID,PCI_DEVICE_ID_VIRTIO_10_BASE + virtio_bus_get_vdev_id(bus));pci_config_set_revision(config, 1);}config[PCI_INTERRUPT_PIN] = 1;if (modern) {struct virtio_pci_cap cap = {.cap_len = sizeof cap,};struct virtio_pci_notify_cap notify = {.cap.cap_len = sizeof notify,.notify_off_multiplier =cpu_to_le32(virtio_pci_queue_mem_mult(proxy)),};struct virtio_pci_cfg_cap cfg = {.cap.cap_len = sizeof cfg,.cap.cfg_type = VIRTIO_PCI_CAP_PCI_CFG,};struct virtio_pci_notify_cap notify_pio = {.cap.cap_len = sizeof notify,.notify_off_multiplier = cpu_to_le32(0x0),};struct virtio_pci_cfg_cap *cfg_mask;virtio_pci_modern_regions_init(proxy, vdev->name);virtio_pci_modern_mem_region_map(proxy, &proxy->common, &cap);virtio_pci_modern_mem_region_map(proxy, &proxy->isr, &cap);virtio_pci_modern_mem_region_map(proxy, &proxy->device, &cap);virtio_pci_modern_mem_region_map(proxy, &proxy->notify, ¬ify.cap);if (modern_pio) {memory_region_init(&proxy->io_bar, OBJECT(proxy),"virtio-pci-io", 0x4);pci_register_bar(&proxy->pci_dev, proxy->modern_io_bar_idx,PCI_BASE_ADDRESS_SPACE_IO, &proxy->io_bar);virtio_pci_modern_io_region_map(proxy, &proxy->notify_pio,¬ify_pio.cap);}pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar_idx,PCI_BASE_ADDRESS_SPACE_MEMORY |PCI_BASE_ADDRESS_MEM_PREFETCH |PCI_BASE_ADDRESS_MEM_TYPE_64,&proxy->modern_bar);proxy->config_cap = virtio_pci_add_mem_cap(proxy, &cfg.cap);cfg_mask = (void *)(proxy->pci_dev.wmask + proxy->config_cap);pci_set_byte(&cfg_mask->cap.bar, ~0x0);pci_set_long((uint8_t *)&cfg_mask->cap.offset, ~0x0);pci_set_long((uint8_t *)&cfg_mask->cap.length, ~0x0);pci_set_long(cfg_mask->pci_cfg_data, ~0x0);}if (proxy->nvectors) {int err = msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors,proxy->msix_bar_idx, NULL);if (err) {/* Notice when a system that supports MSIx can't initialize it */if (err != -ENOTSUP) {warn_report("unable to init msix vectors to %" PRIu32,proxy->nvectors);}proxy->nvectors = 0;}}proxy->pci_dev.config_write = virtio_write_config;proxy->pci_dev.config_read = virtio_read_config;if (legacy) {size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)+ virtio_bus_get_vdev_config_len(bus);size = pow2ceil(size);memory_region_init_io(&proxy->bar, OBJECT(proxy),&virtio_pci_config_ops,proxy, "virtio-pci", size);pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar_idx,PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);}
}
(3)virtio_pci_modern_regions_init函数初始化5个MemoryRegion,分别是virtio-pci-common、virtio-pci-isr、virtio-pci-device、virtio-pci-notify和virtio-pci-notify-pio。代码片段如下:
virtio_pci_modern_regions_init(proxy, vdev->name);
virtio_pci_modern_regions_init函数在hw/virtio/virtio-pci.c中,代码如下:
static void virtio_pci_modern_regions_init(VirtIOPCIProxy *proxy,const char *vdev_name)
{static const MemoryRegionOps common_ops = {.read = virtio_pci_common_read,.write = virtio_pci_common_write,.impl = {.min_access_size = 1,.max_access_size = 4,},.endianness = DEVICE_LITTLE_ENDIAN,};static const MemoryRegionOps isr_ops = {.read = virtio_pci_isr_read,.write = virtio_pci_isr_write,.impl = {.min_access_size = 1,.max_access_size = 4,},.endianness = DEVICE_LITTLE_ENDIAN,};static const MemoryRegionOps device_ops = {.read = virtio_pci_device_read,.write = virtio_pci_device_write,.impl = {.min_access_size = 1,.max_access_size = 4,},.endianness = DEVICE_LITTLE_ENDIAN,};static const MemoryRegionOps notify_ops = {.read = virtio_pci_notify_read,.write = virtio_pci_notify_write,.impl = {.min_access_size = 1,.max_access_size = 4,},.endianness = DEVICE_LITTLE_ENDIAN,};static const MemoryRegionOps notify_pio_ops = {.read = virtio_pci_notify_read,.write = virtio_pci_notify_write_pio,.impl = {.min_access_size = 1,.max_access_size = 4,},.endianness = DEVICE_LITTLE_ENDIAN,};g_autoptr(GString) name = g_string_new(NULL);g_string_printf(name, "virtio-pci-common-%s", vdev_name);memory_region_init_io(&proxy->common.mr, OBJECT(proxy),&common_ops,proxy,name->str,proxy->common.size);g_string_printf(name, "virtio-pci-isr-%s", vdev_name);memory_region_init_io(&proxy->isr.mr, OBJECT(proxy),&isr_ops,proxy,name->str,proxy->isr.size);g_string_printf(name, "virtio-pci-device-%s", vdev_name);memory_region_init_io(&proxy->device.mr, OBJECT(proxy),&device_ops,proxy,name->str,proxy->device.size);g_string_printf(name, "virtio-pci-notify-%s", vdev_name);memory_region_init_io(&proxy->notify.mr, OBJECT(proxy),¬ify_ops,proxy,name->str,proxy->notify.size);g_string_printf(name, "virtio-pci-notify-pio-%s", vdev_name);memory_region_init_io(&proxy->notify_pio.mr, OBJECT(proxy),¬ify_pio_ops,proxy,name->str,proxy->notify_pio.size);
}
VirtIOPCIProxy结构的定义在include/hw/virtio/virtio-pci.h中,如下:
struct VirtIOPCIProxy {PCIDevice pci_dev;MemoryRegion bar;union {struct {VirtIOPCIRegion common;VirtIOPCIRegion isr;VirtIOPCIRegion device;VirtIOPCIRegion notify;VirtIOPCIRegion notify_pio;};VirtIOPCIRegion regs[5];};MemoryRegion modern_bar;MemoryRegion io_bar;uint32_t legacy_io_bar_idx;uint32_t msix_bar_idx;uint32_t modern_io_bar_idx;uint32_t modern_mem_bar_idx;int config_cap;uint32_t flags;bool disable_modern;bool ignore_backend_features;OnOffAuto disable_legacy;/* Transitional device id */uint16_t trans_devid;uint32_t class_code;uint32_t nvectors;uint32_t dfselect;uint32_t gfselect;uint32_t guest_features[2];VirtIOPCIQueue vqs[VIRTIO_QUEUE_MAX];VirtIOIRQFD *vector_irqfd;int nvqs_with_notifiers;VirtioBusState bus;
};
virtio-pci-common、virtio-pci-isr、virtio-pci-device、virtio-pci-notify和virtio-pci-notify-pio这5个MemoryRegion的相关信息存放在VirtIOPCIProxy结构中的几个VirtIOPCIRegion成员中。
(4)virtio_pci_device_plugged接下来调用virtio_pci_modern_mem_region_map函。代码片段如下:
virtio_pci_modern_mem_region_map(proxy, &proxy->common, &cap);virtio_pci_modern_mem_region_map(proxy, &proxy->isr, &cap);virtio_pci_modern_mem_region_map(proxy, &proxy->device, &cap);virtio_pci_modern_mem_region_map(proxy, &proxy->notify, ¬ify.cap);
virtio_pci_modern_mem_region_map函数在hw/virtio/virtio-pci.c中,代码如下:
static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy *proxy,VirtIOPCIRegion *region,struct virtio_pci_cap *cap)
{virtio_pci_modern_region_map(proxy, region, cap,&proxy->modern_bar, proxy->modern_mem_bar_idx);
}
virtio_pci_modern_mem_region_map函数仅仅调用了virtio_pci_modern_region_map函数。该函数也在hw/virtio/virtio-pci.c中(就在上边),代码如下:
static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy,VirtIOPCIRegion *region,struct virtio_pci_cap *cap,MemoryRegion *mr,uint8_t bar)
{memory_region_add_subregion(mr, region->offset, ®ion->mr);cap->cfg_type = region->type;cap->bar = bar;cap->offset = cpu_to_le32(region->offset);cap->length = cpu_to_le32(region->size);virtio_pci_add_mem_cap(proxy, cap);}
virtio_pci_modern_region_map函数完成了两个功能:
1)将VirtIOPCIRegion的mr成员virtio-pci-***作为子MemoryRegion加入到VirtIOProxy的modern_bar成员中去。所以当在虚拟机内部写virtio PCI proxy的MMIO时会落入这几个virtio设备的MemoryRegion的回调函数。
2)调用virtio_pci_add_mem_cap函数将这些寄存器信息加入到virtio PCI代理设备的pci capability上去。virtio_pci_add_mem_cap函数同样在hw/virtio/virtio-pci.c中,代码如下:
static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,struct virtio_pci_cap *cap)
{PCIDevice *dev = &proxy->pci_dev;int offset;offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0,cap->cap_len, &error_abort);assert(cap->cap_len >= sizeof *cap);memcpy(dev->config + offset + PCI_CAP_FLAGS, &cap->cap_len,cap->cap_len - PCI_CAP_FLAGS);return offset;
}
virtio_pci_device_plugged函数的其余部分,将在下一回继续解析。
这篇关于QEMU源码全解析 —— virtio(15)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!