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

88 篇文章 19 订阅
本文深入剖析QEMU中virtio PCI设备的实现过程,从device_set_realized函数开始,详细解读virtio_pci_dc_realize、pci_qdev_realize以及virtio_pci_realize函数的内部逻辑,包括设备注册、配置空间初始化、BAR数据初始化和VirtIOPCIRegion的设置,逐步揭示virtio设备如何在QEMU中模拟运行。
摘要由CSDN通过智能技术生成

接前一篇文章:

上两回讲解了 virtio balloon相关类所涉及的realize函数以及大致流程,如下表所示:

realize函数 parent_dc_realize函数
DeviceClass virtio_pci_dc_realize
PCIDeviceClass virtio_pci_realize
VirtioPCIClass virtio_balloon_pci_realize pci_qdev_realize

本回对于流程进行深入解析。

综合前文书所讲,当具现化TYPE_VIRTIO_BALLOON的时候,device_set_realized函数中会首先调用DeciceClass->realize即virtio_pci_dc_realize函数。再次贴出该函数源码,在hw/virtio/virtio-pci.c中,如下:

  1. static void virtio_pci_dc_realize(DeviceState *qdev, Error **errp)
  2. {
  3. VirtioPCIClass *vpciklass = VIRTIO_PCI_GET_CLASS(qdev);
  4. VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
  5. PCIDevice *pci_dev = &proxy->pci_dev;
  6. if (!(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_PCIE) &&
  7. virtio_pci_modern(proxy)) {
  8. pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
  9. }
  10. vpciklass->parent_dc_realize(qdev, errp);
  11. }

virtio_pci_dc_realize函数首先判断virtio PCI代理设备是否具有VIRTIO_PCI_FLAG_DISABLE_PCIE特性,该特性使得virtio PCI代理展现出 PCIe 的的接口。

然后调用了vpciklass->parent_dc_realize函数,由前文分析可知,该回调函数是pci_qdev_realize()。再次贴出pci_qdev_realize函数源码,在hw/pci/pci.c中,如下:

  1. static void pci_qdev_realize(DeviceState *qdev, Error **errp)
  2. {
  3. PCIDevice *pci_dev = (PCIDevice *)qdev;
  4. PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
  5. ObjectClass *klass = OBJECT_CLASS(pc);
  6. Error *local_err = NULL;
  7. bool is_default_rom;
  8. uint16_t class_id;
  9. /*
  10. * capped by systemd (see: udev-builtin-net_id.c)
  11. * as it's the only known user honor it to avoid users
  12. * misconfigure QEMU and then wonder why acpi-index doesn't work
  13. */
  14. if (pci_dev->acpi_index > ONBOARD_INDEX_MAX) {
  15. error_setg(errp, "acpi-index should be less or equal to %u",
  16. ONBOARD_INDEX_MAX);
  17. return;
  18. }
  19. /*
  20. * make sure that acpi-index is unique across all present PCI devices
  21. */
  22. if (pci_dev->acpi_index) {
  23. GSequence *used_indexes = pci_acpi_index_list();
  24. if (g_sequence_lookup(used_indexes,
  25. GINT_TO_POINTER(pci_dev->acpi_index),
  26. g_cmp_uint32, NULL)) {
  27. error_setg(errp, "a PCI device with acpi-index = %" PRIu32
  28. " already exist", pci_dev->acpi_index);
  29. return;
  30. }
  31. g_sequence_insert_sorted(used_indexes,
  32. GINT_TO_POINTER(pci_dev->acpi_index),
  33. g_cmp_uint32, NULL);
  34. }
  35. if (pci_dev->romsize != -1 && !is_power_of_2(pci_dev->romsize)) {
  36. error_setg(errp, "ROM size %u is not a power of two", pci_dev->romsize);
  37. return;
  38. }
  39. /* initialize cap_present for pci_is_express() and pci_config_size(),
  40. * Note that hybrid PCIs are not set automatically and need to manage
  41. * QEMU_PCI_CAP_EXPRESS manually */
  42. if (object_class_dynamic_cast(klass, INTERFACE_PCIE_DEVICE) &&
  43. !object_class_dynamic_cast(klass, INTERFACE_CONVENTIONAL_PCI_DEVICE)) {
  44. pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
  45. }
  46. if (object_class_dynamic_cast(klass, INTERFACE_CXL_DEVICE)) {
  47. pci_dev->cap_present |= QEMU_PCIE_CAP_CXL;
  48. }
  49. pci_dev = do_pci_register_device(pci_dev,
  50. object_get_typename(OBJECT(qdev)),
  51. pci_dev->devfn, errp);
  52. if (pci_dev == NULL)
  53. return;
  54. if (pc->realize) {
  55. pc->realize(pci_dev, &local_err);
  56. if (local_err) {
  57. error_propagate(errp, local_err);
  58. do_pci_unregister_device(pci_dev);
  59. return;
  60. }
  61. }
  62. /*
  63. * A PCIe Downstream Port that do not have ARI Forwarding enabled must
  64. * associate only Device 0 with the device attached to the bus
  65. * representing the Link from the Port (PCIe base spec rev 4.0 ver 0.3,
  66. * sec 7.3.1).
  67. * With ARI, PCI_SLOT() can return non-zero value as the traditional
  68. * 5-bit Device Number and 3-bit Function Number fields in its associated
  69. * Routing IDs, Requester IDs and Completer IDs are interpreted as a
  70. * single 8-bit Function Number. Hence, ignore ARI capable devices.
  71. */
  72. if (pci_is_express(pci_dev) &&
  73. !pcie_find_capability(pci_dev, PCI_EXT_CAP_ID_ARI) &&
  74. pcie_has_upstream_port(pci_dev) &&
  75. PCI_SLOT(pci_dev->devfn)) {
  76. warn_report("PCI: slot %d is not valid for %s,"
  77. " parent device only allows plugging into slot 0.",
  78. PCI_SLOT(pci_dev->devfn), pci_dev->name);
  79. }
  80. if (pci_dev->failover_pair_id) {
  81. if (!pci_bus_is_express(pci_get_bus(pci_dev))) {
  82. error_setg(errp, "failover primary device must be on "
  83. "PCIExpress bus");
  84. pci_qdev_unrealize(DEVICE(pci_dev));
  85. return;
  86. }
  87. class_id = pci_get_word(pci_dev->config + PCI_CLASS_DEVICE);
  88. if (class_id != PCI_CLASS_NETWORK_ETHERNET) {
  89. error_setg(errp, "failover primary device is not an "
  90. "Ethernet device");
  91. pci_qdev_unrealize(DEVICE(pci_dev));
  92. return;
  93. }
  94. if ((pci_dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)
  95. || (PCI_FUNC(pci_dev->devfn) != 0)) {
  96. error_setg(errp, "failover: primary device must be in its own "
  97. "PCI slot");
  98. pci_qdev_unrealize(DEVICE(pci_dev));
  99. return;
  100. }
  101. qdev->allow_unplug_during_migration = true;
  102. }
  103. /* rom loading */
  104. is_default_rom = false;
  105. if (pci_dev->romfile == NULL && pc->romfile != NULL) {
  106. pci_dev->romfile = g_strdup(pc->romfile);
  107. is_default_rom = true;
  108. }
  109. pci_add_option_rom(pci_dev, is_default_rom, &local_err);
  110. if (local_err) {
  111. error_propagate(errp, local_err);
  112. pci_qdev_unrealize(DEVICE(pci_dev));
  113. return;
  114. }
  115. pci_set_power(pci_dev, true);
  116. pci_dev->msi_trigger = pci_msi_trigger;
  117. }

pci_qdev_realize函数会将virtioPCI代理设备注册到PCI总线上,如前文所示:

并调用PCIDeviceClass->realize函数指针所指向的函数,也就是virtio_pci_realize函数。代码片段如下:

  1. pci_dev = do_pci_register_device(pci_dev,
  2. object_get_typename(OBJECT(qdev)),
  3. pci_dev->devfn, errp);
  4. if (pci_dev == NULL)
  5. return;
  6. if (pc->realize) {
  7. pc->realize(pci_dev, &local_err);
  8. if (local_err) {
  9. error_propagate(errp, local_err);
  10. do_pci_unregister_device(pci_dev);
  11. return;
  12. }
  13. }

关于pci_qdev_realize函数的详细讲解可以参阅笔者的博文 QEMU源码全解析 —— PCI设备模拟(2) 。这里为了便于理解,将其中部分内容再次贴出:

pci_qdev_realize函数主要包括三个方面的工作:

(1)首先调用do_pci_register_device函数进行注册。

do_pci_register_device函数完成设备及其对应PCI总线上的一些初始化工作。具体包括:

1)如果指定的devfn为-1,表示由总线自己选择插槽,得到插槽之后保存在PCIDevice的devfn(即pci_dev->devfn)中;如果在设备命令行中指定了addr,则addr会作为设备的devfn。

2)接下来设置PCIDevice结构体中的各个域,包括调用pci_init_bus_master函数初始化PCIDevice中的 Address 成员bus_master_as及其对应的MR。

3)之后,调用pci_config_alloc函数分配PCI设备的配置空间,cmask用来检测相关的能力,wmask用来控制读写,w1cmask用来实现RW1C。由此完成一些初始化的设置,如vendor_id等。

4)然后是设置设备的config_read和config_write函数。如果相关的子类自己没有设置,那么就使用默认的pci_default_read/write_config函数。

5)最后,将该device复制到bus->devices数组中。

(2)其次,pci_qdev_realize函数调用PCI设备所属的class的realize函数,即pc->realize函数,也就是virtio_pci_realize函数

(3)最后,调用pci_add_option_rom函数加载PCI设备的ROM。

virtio_pci_realize函数在hw/virtio/virtio-pci.c中,代码如下:

  1. static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
  2. {
  3. VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
  4. VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
  5. bool pcie_port = pci_bus_is_express(pci_get_bus(pci_dev)) &&
  6. !pci_bus_is_root(pci_get_bus(pci_dev));
  7. if (kvm_enabled() && !kvm_has_many_ioeventfds()) {
  8. proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
  9. }
  10. /* fd-based ioevents can't be synchronized in record/replay */
  11. if (replay_mode != REPLAY_MODE_NONE) {
  12. proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
  13. }
  14. /*
  15. * virtio pci bar layout used by default.
  16. * subclasses can re-arrange things if needed.
  17. *
  18. * region 0 -- virtio legacy io bar
  19. * region 1 -- msi-x bar
  20. * region 2 -- virtio modern io bar (off by default)
  21. * region 4+5 -- virtio modern memory (64bit) bar
  22. *
  23. */
  24. proxy->legacy_io_bar_idx = 0;
  25. proxy->msix_bar_idx = 1;
  26. proxy->modern_io_bar_idx = 2;
  27. proxy->modern_mem_bar_idx = 4;
  28. proxy->common.offset = 0x0;
  29. proxy->common.size = 0x1000;
  30. proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG;
  31. proxy->isr.offset = 0x1000;
  32. proxy->isr.size = 0x1000;
  33. proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG;
  34. proxy->device.offset = 0x2000;
  35. proxy->device.size = 0x1000;
  36. proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG;
  37. proxy->notify.offset = 0x3000;
  38. proxy->notify.size = virtio_pci_queue_mem_mult(proxy) * VIRTIO_QUEUE_MAX;
  39. proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
  40. proxy->notify_pio.offset = 0x0;
  41. proxy->notify_pio.size = 0x4;
  42. proxy->notify_pio.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
  43. /* subclasses can enforce modern, so do this unconditionally */
  44. memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci",
  45. /* PCI BAR regions must be powers of 2 */
  46. pow2ceil(proxy->notify.offset + proxy->notify.size));
  47. if (proxy->disable_legacy == ON_OFF_AUTO_AUTO) {
  48. proxy->disable_legacy = pcie_port ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
  49. }
  50. if (!virtio_pci_modern(proxy) && !virtio_pci_legacy(proxy)) {
  51. error_setg(errp, "device cannot work as neither modern nor legacy mode"
  52. " is enabled");
  53. error_append_hint(errp, "Set either disable-modern or disable-legacy"
  54. " to off\n");
  55. return;
  56. }
  57. if (pcie_port && pci_is_express(pci_dev)) {
  58. int pos;
  59. uint16_t last_pcie_cap_offset = PCI_CONFIG_SPACE_SIZE;
  60. pos = pcie_endpoint_cap_init(pci_dev, 0);
  61. assert(pos > 0);
  62. pos = pci_add_capability(pci_dev, PCI_CAP_ID_PM, 0,
  63. PCI_PM_SIZEOF, errp);
  64. if (pos < 0) {
  65. return;
  66. }
  67. pci_dev->exp.pm_cap = pos;
  68. /*
  69. * Indicates that this function complies with revision 1.2 of the
  70. * PCI Power Management Interface Specification.
  71. */
  72. pci_set_word(pci_dev->config + pos + PCI_PM_PMC, 0x3);
  73. if (proxy->flags & VIRTIO_PCI_FLAG_AER) {
  74. pcie_aer_init(pci_dev, PCI_ERR_VER, last_pcie_cap_offset,
  75. PCI_ERR_SIZEOF, NULL);
  76. last_pcie_cap_offset += PCI_ERR_SIZEOF;
  77. }
  78. if (proxy->flags & VIRTIO_PCI_FLAG_INIT_DEVERR) {
  79. /* Init error enabling flags */
  80. pcie_cap_deverr_init(pci_dev);
  81. }
  82. if (proxy->flags & VIRTIO_PCI_FLAG_INIT_LNKCTL) {
  83. /* Init Link Control Register */
  84. pcie_cap_lnkctl_init(pci_dev);
  85. }
  86. if (proxy->flags & VIRTIO_PCI_FLAG_INIT_PM) {
  87. /* Init Power Management Control Register */
  88. pci_set_word(pci_dev->wmask + pos + PCI_PM_CTRL,
  89. PCI_PM_CTRL_STATE_MASK);
  90. }
  91. if (proxy->flags & VIRTIO_PCI_FLAG_ATS) {
  92. pcie_ats_init(pci_dev, last_pcie_cap_offset,
  93. proxy->flags & VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED);
  94. last_pcie_cap_offset += PCI_EXT_CAP_ATS_SIZEOF;
  95. }
  96. if (proxy->flags & VIRTIO_PCI_FLAG_INIT_FLR) {
  97. /* Set Function Level Reset capability bit */
  98. pcie_cap_flr_init(pci_dev);
  99. }
  100. } else {
  101. /*
  102. * make future invocations of pci_is_express() return false
  103. * and pci_config_size() return PCI_CONFIG_SPACE_SIZE.
  104. */
  105. pci_dev->cap_present &= ~QEMU_PCI_CAP_EXPRESS;
  106. }
  107. virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
  108. if (k->realize) {
  109. k->realize(proxy, errp);
  110. }
  111. }

(1)virtio_pci_realize函数首先得到virtioPCIProxy设备,也就是VirtIOPCIProxy结构的对象。代码片段如下:

    VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);

(2)然后,virtio_pci_realize函数初始化VirtIOPCIProxy设备的多个BAR数据,设置了这些BAR的索引号。代码片段如下:

  1. /*
  2. * virtio pci bar layout used by default.
  3. * subclasses can re-arrange things if needed.
  4. *
  5. * region 0 -- virtio legacy io bar
  6. * region 1 -- msi-x bar
  7. * region 2 -- virtio modern io bar (off by default)
  8. * region 4+5 -- virtio modern memory (64bit) bar
  9. *
  10. */
  11. proxy->legacy_io_bar_idx = 0;
  12. proxy->msix_bar_idx = 1;
  13. proxy->modern_io_bar_idx = 2;
  14. proxy->modern_mem_bar_idx = 4;

其中:

legacy I/O地址为0;

msi-x地址为1;

modern IO地址为2;

modern MMIO地址为4。

这里的legacy和modern指的是不同的virtio版本。

(3)virtio_pci_realize函数还初始化了多个VirtIOPCIRegion(VirtIOPCIRegion用来表示virtio设备的配置空间信息),如VirtIOPCIProxy的common、isr、device、notify成员。代码片段如下:

  1. proxy->common.offset = 0x0;
  2. proxy->common.size = 0x1000;
  3. proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG;
  4. proxy->isr.offset = 0x1000;
  5. proxy->isr.size = 0x1000;
  6. proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG;
  7. proxy->device.offset = 0x2000;
  8. proxy->device.size = 0x1000;
  9. proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG;
  10. proxy->notify.offset = 0x3000;
  11. proxy->notify.size = virtio_pci_queue_mem_mult(proxy) * VIRTIO_QUEUE_MAX;
  12. proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
  13. proxy->notify_pio.offset = 0x0;
  14. proxy->notify_pio.size = 0x4;
  15. proxy->notify_pio.type = VIRTIO_PCI_CAP_NOTIFY_CFG;

VirtIOPCIRegion保存了VirtIOPCIProxy设备modern MMIO的相关信息。如VirtIOProxy的modern MMIO中,最开始的区域是common区域,其大小为0x1000,范围是[0x0, 0x1000);接着是isr区域,大小也是0x1000,范围是[0x1000, 0x2000);然后是device区域,其大小仍然是0x1000,范围是[0x2000,0x3000);最后是notify区域,其大小是virtio_pci_queue_mem_mult(proxy) * VIRTIO_QUEUE_MAX,范围是[0x3000, 0x3000+virtio_pci_queue_mem_mult(proxy) * VIRTIO_QUEUE_MAX)。

(4)VirtIOPCIProxy的modern MMIO对应的MemoryRegion存放在VirtIOPCIProxy的modern_bar成员中;它还有一个MemoryRegion存放在modern_cfg成员中(这部分老版本代码有,新版本代码已经没有了)。代码片段如下:

  1. /* subclasses can enforce modern, so do this unconditionally */
  2. memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci",
  3. /* PCI BAR regions must be powers of 2 */
  4. pow2ceil(proxy->notify.offset + proxy->notify.size));
  5. if (proxy->disable_legacy == ON_OFF_AUTO_AUTO) {
  6. proxy->disable_legacy = pcie_port ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
  7. }
  8. if (!virtio_pci_modern(proxy) && !virtio_pci_legacy(proxy)) {
  9. error_setg(errp, "device cannot work as neither modern nor legacy mode"
  10. " is enabled");
  11. error_append_hint(errp, "Set either disable-modern or disable-legacy"
  12. " to off\n");
  13. return;
  14. }

(5)virtio_pci_realize函数会调用virtio_pci_bus_new函数创建virtio-bus,挂载到当前的virtio PCI代理设备下面。代码片段如下:

    virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);

(6)virtio_pci_realize函数在最后调用了k->realize函数指针所指向的函数。由于k是VirtioPCIClass类型,因此根据前表

realize函数 parent_dc_realize函数
DeviceClass virtio_pci_dc_realize
PCIDeviceClass virtio_pci_realize
VirtioPCIClass virtio_balloon_pci_realize pci_qdev_realize

因此k->realize函数指针实际指向了virtio_balloon_pci_realize函数。

对于virtio_balloon_pci_realize函数内容的讲解,请看下回。

举报

选择你想要举报的内容(必选)
  • 内容涉黄
  • 政治相关
  • 内容抄袭
  • 涉嫌广告
  • 内容侵权
  • 侮辱谩骂
  • 样式问题
  • 其他