Provide a Common Layer for semu

未关闭
#145 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
重构
描述清晰度
基本清楚
活跃度
活跃
技术栈
c, linux

调研方向

Start by reading the cited virtio device files, especially virtio-net.c, virtio-blk.c, virtio-snd.c, and virtio-gpu.c, alongside the referenced QEMU VirtIO core and MMIO transport. The issue describes duplicated queue, register, guest-memory, and lifecycle handling, but does not define a specific common-layer design or a bounded first change. Done would require agreeing on an architecture and migrating device implementations incrementally; the issue does not specify an initial migration target or completion criteria.

由索引模型根据 Issue 内容生成。

描述

Provide a Common Layer for semu

Support for virtio-gpu introduced a multithreaded model to semu, while the current SMP implementation uses coroutine-based cooperative multitasking. Guest Linux can see multiple harts, but on the host they still take turns running in the same emulator thread (see main.c:2000-2025 and main.c:1713-1726).

Since we have only recently introduced multithreading, I think this is a good time to design multi-core emulation.

Before that, however, I think there is a problem we need to address. Some common operations have not been factored out into shared implementations. Instead, each virtio device implements them separately, for example:

  • MMIO registers for status, queue selection, size, addresses, and notifications

  • Descriptor traversal and request segmentation

    • FS always reads four descriptors, while the SND control queue collects at most three. Both handle descriptor indices and chaining themselves.
    • The GPU's three-descriptor limit relies on the common Linux request/data/response layout. Command data allocated with vmalloc may require more descriptors than this limit allows.
  • avail/used indices, completion entries, and notifications

    • SND, NET, and other devices each write used entries, advance indices, and publish completion notifications themselves.
    • RNG does not first check whether the avail ring contains a new request, so it may consume an old head again. It also modifies the guest descriptor's flags.
    • BLK does not publish used completions for out-of-bounds or unsupported requests, so the guest cannot reclaim those requests normally.
  • Guest-memory address translation, bounds checking, and data copying

    • The CPU has ram_read/ram_write, while devices also obtain RAM pointers and access RAM directly.
    • ram_write implements byte/halfword writes by reading, modifying, and writing back an entire word. CPU instruction fetch and page-table updates also still have paths that access RAM directly.
    • The GPU address-translation function accepts a descriptor's 64-bit address through a uint32_t parameter, truncating the upper bits before bounds checking and potentially accepting an address outside RAM as valid.
  • Reset and resource lifetimes

In QEMU, these operations are handled by the shared VirtIO core, the virtio-mmio transport, and the AddressSpace/DMA interfaces:

I think this would be a major obstacle to implementing multi-core emulation. For example, multiple harts could modify the GPU's queue state concurrently through the same MMIO handler (main.c:440-444), and the CPU and device worker threads could also access guest RAM concurrently. If each device continues to handle these operations separately, we will have to identify and address the concurrent entry points in every device individually. #143, opened by @Cuda-Chen recently, also falls into this category.

I tried addressing all of these issues at once on my own branch, but that did not work well. There are already quite a few virtio devices, so doing everything at once requires many changes to both the devices and the emulator itself, making review very difficult.

In a sense, this would be an architectural reorganization. I think we could develop the common layer and the individual virtio devices separately and in parallel. Each virtio device could initially keep its existing duplicated implementation. Once the common layer is ready, we could gradually open PRs to migrate one virtio device at a time to an implementation based on the common layer.

Once all of this is complete, I think we can begin implementing multi-core emulation.

主要语言
C
星标
316
派生
76
平均合并
7 小时 1 分钟
30 天内合并 PR
1

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

sysprog21/semu 的其他 Issue

查看 sysprog21/semu 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。