Provide a Common Layer for semu
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 25/100
- Loại issue
- Tái cấu trúc
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- c, linux
- Lĩnh vực
- backend, devtools, operating-systems
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- NET, INPUT, and other virtio devices each implement their own handling of
QueueReady,QueueNotify,InterruptACK, and other registers. - BLK config bounds checks mix word indices with byte sizes, potentially allowing accesses beyond the config object.
- See virtio-blk.c:418-426 and virtio-blk.c:327-333.
- NET, INPUT, and other virtio devices each implement their own handling of
-
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.
- See virtio-fs.c:617-629 and virtio-snd.c:858-888.
- The GPU's three-descriptor limit relies on the common Linux request/data/response layout. Command data allocated with
vmallocmay require more descriptors than this limit allows.- See virtio-gpu.h:18-40 and virtio-gpu.c:667-697.
- FS always reads four descriptors, while the SND control queue collects at most three. Both handle descriptor indices and chaining themselves.
-
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.- See virtio-rng.c:50-95.
- BLK does not publish used completions for out-of-bounds or unsupported requests, so the guest cannot reclaim those requests normally.
- See virtio-blk.c:199-218 and virtio-blk.c:265-282.
-
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_writeimplements 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.- See ram.c:46-63 and riscv.c:468-529.
- The GPU address-translation function accepts a descriptor's 64-bit address through a
uint32_tparameter, truncating the upper bits before bounds checking and potentially accepting an address outside RAM as valid.- See virtio.h:113-118 and virtio-gpu.c:65-95.
- The CPU has
-
Reset and resource lifetimes
- SND reset clears the device state directly without waiting for the TX worker, which may still be processing a queue. Both paths may access the same state concurrently.
- SND's
PCM_RELEASEcontrol path and TX worker may process the same queue concurrently without mutual exclusion. - If audio backend initialization fails, SND does not clean up the TX worker it has already created.
In QEMU, these operations are handled by the shared VirtIO core, the virtio-mmio transport, and the AddressSpace/DMA interfaces:
-
Descriptor and avail-ring handling in the shared VirtIO core
- The split-ring path in
virtqueue_popobtains the head, validates indices, walks the chain, and builds scatter/gather mappings. - GPU uses
virtqueue_popto obtain aVirtQueueElement, then processes its own commands.
- The split-ring path in
-
Completion entry writes and publication
virtqueue_push/virtqueue_fill/virtqueue_flushprovide shared handling and execute the necessary memory barriers before updating the used index.- GPU and BLK each write their responses and determine the completion length before calling these helpers.
-
The
QueueReady,QueueNotify,InterruptACK, and other registers- These are handled by the virtio-mmio transport.
virtio_notifychecks notification-suppression conditions to decide whether to notify the guest after completion, then notifies the transport through the bus to update the IRQ.
-
Shared entry points for guest address-space access and DMA mapping
address_space_read_full/address_space_writehandleAddressSpaceaccesses. VirtIO descriptor mapping goes through the device'sdma_asto calldma_memory_map.- This allows devices to reuse the address-space and mapping mechanisms.
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.
- Ngôn ngữ chính
- C
- Star
- 316
- Fork
- 76
- Merge trung bình
- 7 giờ 1 phút
- Pull request đã merge (30 ngày)
- 1
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của sysprog21/semu
-
Implement virtio-gpu 3DĐang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
-
Implement multi-core emulationĐang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 42/100
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
-
Implement VirtIO PCI transport layerCó thể làm lại được @shengwen-tw đã nhận 527 ngày trước và không có pull request nào đang mở. Đang mở
Tất cả issue của sysprog21/semu
Issue tương tự
-
issue: bug report
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 88/100
BasedHardware/omi#26486 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
The LLVM object can build before collection_config.h is generatedCó thể đã có người làm @Vonng đã nhận hôm nay. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
aws/pgcollection#51 ·
-
area/docdb kind/failing-test priority/medium status/awaiting-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
yugabyte/yugabyte-db#39462 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
libreswan/libreswan#3185 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày