Il mer 21 mag 2025, 10:37 Paolo Bonzini <bonz...@gnu.org> ha scritto:
> > So... not sure what to do there. It seems like vm-memory is very close > to > >> > being usable by QEMU, but maybe not completely. :( >> >> Is it possible or necessary for vm-memory to support overlap? Because I >> feel that if it is possible, the problem might be simplified. (As a >> beginner, I have yet to understand exactly how difficult it is.) >> > > I don't think that's necessary. Just like in QEMU C code we have > AddressSpace for DMA and MemoryRegion for hierarchy, in Rust code you have > qemu_api::{AddressSpace,MemoryRegion}. FlatView, FlatRange, > MemoryRegionSection are hidden in both cases, and users don't care much > about which type implements GuestMemoryRegion because all they see is > AddressSpace. Again, it's all hidden behind the prelude. > > The real problem is how hard it is to remove the references from the > vm-memory API... Maybe not much. > Brain dump ahead! AddressSpaceDispatch is already storing MemoryRegionSections. Therefore it should be possible to make GuestMemory::R equal to MemoryRegionSection, or rather its Opaque wrapper. Then one needs to implement Bytes<MemoryRegionAddress> in MemoryRegionSection like https://github.com/rust-vmm/vm-memory/blob/3f2fd80b11/src/mmap/mod.rs#L171, use address_space_lookup_region() to implement find_region(), and (except for IOMMU) everything should work. To implement IOMMUs later, it's probably possible to call flatview_translate() from the try_access() method, which is intended to be internal but comes in handy here. try_access() is a bit complicated, but we'll just have to copy some code from the default implementation at https://github.com/rust-vmm/vm-memory/blob/main/src/guest_memory.rs, in order to replace the call to find_region(). Notice how address_space_lookup_region() returns MemoryRegionSection*, which translates to Option<&Self::R>; while flatview_translate() returns a MemoryRegionSection by value, that can become a local variable in try_access() and everything is fine. Hopefully. The compiler will certainly prove me wrong. Another thing that will be needed later is support for MemTxAttrs. Maybe squeeze those into the address, i.e. implement Bytes<(GuestAddress, MemTxAttrs)> and Bytes<(MemoryRegionAddress, MemTxAttrs)>? The dirty bitmap does need some changes in vm-memory, but it's pretty small compared to the mess that I foreshadowed in order to change find_region(). Paolo