QEMU源码全解析 —— virtio(15)

2023-12-19 11:36
文章标签 源码 15 解析 qemu virtio

本文主要是介绍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, &notify.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,&notify_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),&notify_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),&notify_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, &notify.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, &region->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)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/512010

相关文章

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

Java并发编程必备之Synchronized关键字深入解析

《Java并发编程必备之Synchronized关键字深入解析》本文我们深入探索了Java中的Synchronized关键字,包括其互斥性和可重入性的特性,文章详细介绍了Synchronized的三种... 目录一、前言二、Synchronized关键字2.1 Synchronized的特性1. 互斥2.

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

Java的IO模型、Netty原理解析

《Java的IO模型、Netty原理解析》Java的I/O是以流的方式进行数据输入输出的,Java的类库涉及很多领域的IO内容:标准的输入输出,文件的操作、网络上的数据传输流、字符串流、对象流等,这篇... 目录1.什么是IO2.同步与异步、阻塞与非阻塞3.三种IO模型BIO(blocking I/O)NI

Python 中的异步与同步深度解析(实践记录)

《Python中的异步与同步深度解析(实践记录)》在Python编程世界里,异步和同步的概念是理解程序执行流程和性能优化的关键,这篇文章将带你深入了解它们的差异,以及阻塞和非阻塞的特性,同时通过实际... 目录python中的异步与同步:深度解析与实践异步与同步的定义异步同步阻塞与非阻塞的概念阻塞非阻塞同步

Redis中高并发读写性能的深度解析与优化

《Redis中高并发读写性能的深度解析与优化》Redis作为一款高性能的内存数据库,广泛应用于缓存、消息队列、实时统计等场景,本文将深入探讨Redis的读写并发能力,感兴趣的小伙伴可以了解下... 目录引言一、Redis 并发能力概述1.1 Redis 的读写性能1.2 影响 Redis 并发能力的因素二、

Spring MVC使用视图解析的问题解读

《SpringMVC使用视图解析的问题解读》:本文主要介绍SpringMVC使用视图解析的问题解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring MVC使用视图解析1. 会使用视图解析的情况2. 不会使用视图解析的情况总结Spring MVC使用视图

利用Python和C++解析gltf文件的示例详解

《利用Python和C++解析gltf文件的示例详解》gltf,全称是GLTransmissionFormat,是一种开放的3D文件格式,Python和C++是两个非常强大的工具,下面我们就来看看如何... 目录什么是gltf文件选择语言的原因安装必要的库解析gltf文件的步骤1. 读取gltf文件2. 提

Java中的runnable 和 callable 区别解析

《Java中的runnable和callable区别解析》Runnable接口用于定义不需要返回结果的任务,而Callable接口可以返回结果并抛出异常,通常与Future结合使用,Runnab... 目录1. Runnable接口1.1 Runnable的定义1.2 Runnable的特点1.3 使用Ru