Skip to content

[dm][ivshmem] Add Inter-VM shared memory for virtualization#11645

Open
GuEe-GUI wants to merge 1 commit into
RT-Thread:masterfrom
GuEe-GUI:ivshmem
Open

[dm][ivshmem] Add Inter-VM shared memory for virtualization#11645
GuEe-GUI wants to merge 1 commit into
RT-Thread:masterfrom
GuEe-GUI:ivshmem

Conversation

@GuEe-GUI

@GuEe-GUI GuEe-GUI commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

[
Add Inter-VM shared memory for virtualization

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用clang-format 源码格式化工具确保格式符合RT-Thread代码规范 This PR has been formatted with clang-format and complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@github-actions

Copy link
Copy Markdown

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  • 将目标分支设置为 \ Set the target branch to:ivshmem
  • 设置PR number为 \ Set the PR number to:11645
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 ivshmem 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the ivshmem branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions

github-actions Bot commented Jul 22, 2026

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: @Maihuanyi

Changed Files (Click to expand)
  • components/drivers/Kconfig
  • components/drivers/ivshmem/Kconfig
  • components/drivers/ivshmem/SConscript
  • components/drivers/ivshmem/ivshmem-qemu.c
  • components/drivers/ivshmem/ivshmem.c
  • components/drivers/ivshmem/ivshmem.h

📊 Current Review Status (Last Updated: 2026-07-23 09:57 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces an Inter-VM shared memory (ivshmem) device driver under the RT-Thread Device Model (DM) / PCI stack to support virtualization scenarios (including a QEMU-specific implementation).

Changes:

  • Add ivshmem driver Kconfig and build integration under components/drivers/.
  • Introduce a common PCI probe/remove implementation (ivshmem.c/.h) for ivshmem devices.
  • Add a QEMU ivshmem PCI driver with RT-Thread device ops and an MSH helper command (ivshmem-qemu.c).

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
components/drivers/Kconfig Adds Kconfig sourcing for ivshmem (and also adds an ethernet Kconfig include).
components/drivers/ivshmem/SConscript Adds SCons build rules for ivshmem and optional QEMU backend.
components/drivers/ivshmem/Kconfig Adds configuration options for enabling ivshmem and QEMU support.
components/drivers/ivshmem/ivshmem.h Defines ivshmem registers/BARs and core ivshmem device structure/API.
components/drivers/ivshmem/ivshmem.c Implements common PCI probe/remove and IRQ setup helpers.
components/drivers/ivshmem/ivshmem-qemu.c Implements QEMU ivshmem PCI driver, device ops, and MSH command helpers.
Comments suppressed due to low confidence (4)

components/drivers/ivshmem/ivshmem.c:182

  • [bug/缺陷]: Reading 64-bit shared-memory address/size by casting &ivdev->shmem / &ivdev->shmem_size to rt_uint32_t* and using + 1 is non-portable and can break on different architectures/ABIs.

English: This relies on object layout/endianness and may violate strict-aliasing/alignment rules. Prefer reading into temporary u32 low/high parts and composing a 64-bit value explicitly.
中文:通过把 &ivdev->shmem / &ivdev->shmem_size 强转为 rt_uint32_t*+1 读取高 32bit 的方式不具备可移植性,且可能触发严格别名/对齐问题。建议用临时的 low/high 变量读取后显式拼成 64bit 值。

        rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_ADDR_LO, (rt_uint32_t *)&ivdev->shmem);
        rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_ADDR_HI, (rt_uint32_t *)&ivdev->shmem + 1);
        rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_SIZE_LO, (rt_uint32_t *)&ivdev->shmem_size);
        rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_SIZE_HI, (rt_uint32_t *)&ivdev->shmem_size + 1);

components/drivers/ivshmem/ivshmem-qemu.c:234

  • [bug/缺陷]: qemu_ivshmem_remove() does not handle ivshmem_pci_remove() returning RT_NULL, which can lead to a NULL dereference.

English: If pdev is invalid or sysdata is not set, ivshmem_pci_remove() returns RT_NULL, but the code immediately calls raw_to_qemu_ivshmem(ivdev). Add a NULL check and return an error.
中文:当 pdev 非法或 sysdata 未设置时,ivshmem_pci_remove() 会返回 RT_NULL,当前代码随后直接 raw_to_qemu_ivshmem(ivdev) 可能空指针解引用。请增加空指针检查并返回错误。

static rt_err_t qemu_ivshmem_remove(struct rt_pci_device *pdev)
{
    struct ivshmem_device *ivdev = ivshmem_pci_remove(pdev);
    struct qemu_ivshmem *qivdev = raw_to_qemu_ivshmem(ivdev);

    rt_device_unregister(&qivdev->parent.parent);
    rt_free(qivdev);

    return RT_EOK;

components/drivers/ivshmem/ivshmem-qemu.c:366

  • [bug/缺陷]: Format string mismatch: pos is long but printed with %d.

English: This is undefined behavior on some targets. Use %ld (or cast to int if you really want %d).
中文:pos 类型为 long,但用 %d 打印在部分平台上会产生未定义行为。请改用 %ld(或显式转换为 int)。

            rt_kprintf("[%2d] = %02x (%c)\n", pos, value, value);

components/drivers/ivshmem/ivshmem.c:216

  • [bug/缺陷]: ivshmem_pci_remove() enables interrupts during teardown and also detaches the legacy INTx IRQ even when MSI-X was used.

English: In the MSI-X loop it calls rt_hw_interrupt_umask(irq) (enable) right before detaching, which can allow IRQs to fire while resources are being freed. Also rt_hw_interrupt_umask(ivdev->irq) / rt_pic_detach_irq(ivdev->irq, ...) runs unconditionally even though INTx handler was never installed in the MSI-X path. Finally, the INTx teardown path calls rt_pci_irq_unmask() (enable) during remove; it should mask.
中文:ivshmem_pci_remove() 在卸载过程中错误地“使能”中断,并且在 MSI-X 模式下仍无条件卸载 legacy INTx IRQ。MSI-X 循环里在 detach 前调用 rt_hw_interrupt_umask() 可能导致释放资源时仍触发中断;同时 INTx 的 umask/detach 在 MSI-X 路径下并未安装过 handler;另外 INTx teardown 里还调用了 rt_pci_irq_unmask()(使能),卸载时应改为 mask。

    if (ivdev->msix_entries)
    {
        for (int i = 0; i < ivdev->nvectors; ++i)
        {
            int irq = ivdev->msix_entries[i].irq;

Comment thread components/drivers/Kconfig Outdated
Comment thread components/drivers/ivshmem/ivshmem.c
Comment thread components/drivers/ivshmem/ivshmem-qemu.c
Signed-off-by: GuEe-GUI <2991707448@qq.com>
@GuEe-GUI

Copy link
Copy Markdown
Contributor Author

@Rbb666 审核建议已修改完成

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants