</>EmbeddedInterview

How does QEMU model guest memory and MMIO (MemoryRegion and AddressSpace)?

HardVirtualization (QEMU & KVM)

Short answer: QEMU describes the guest's physical address map as a tree of MemoryRegions: RAM regions backed by host memory, I/O regions whose accesses call device callbacks (MemoryRegionOps), aliases, and containers that hold sub-regions at offsets. An AddressSpace (e.g. the system address space each CPU sees, or a device's DMA address space) points to a root region. QEMU flattens the tree into a FlatView (a sorted list of ranges) for fast lookup, and with KVM it registers the RAM ranges with the kernel.

Key functions

Function Creates
memory_region_init_ram() RAM backed by host memory
memory_region_init_rom() read-only memory (e.g. boot ROM)
memory_region_init_io() MMIO region: reads/writes call the MemoryRegionOps callbacks
memory_region_init_alias() a window onto part of another region (e.g. low RAM aliased below 1 MB)
memory_region_add_subregion(parent, offset, child) place a region in the map (with priorities for overlaps)

How an access is handled

  • TCG: a guest load goes through the software TLB. For RAM, the TLB entry gives a host pointer and the access is a plain host load. For MMIO, the slow path finds the MemoryRegion and calls its read callback with the offset and size.
  • KVM: RAM ranges are given to the kernel with KVM_SET_USER_MEMORY_REGION (guest physical address → QEMU's host virtual address). Guest accesses to RAM never leave the guest. An access to an address with no RAM mapping causes a stage-2 fault; KVM reports KVM_EXIT_MMIO to QEMU with the address, size and data, QEMU calls the device's callback, and then resumes the vCPU.

See the map: QEMU monitor info mtree prints the whole tree; info mtree -f prints the flattened view.

address-space: memory
  0000000000000000-ffffffffffffffff (prio 0, i/o): system
    0000000008000000-0000000008020000 (prio 0, i/o): gic_dist
    0000000009000000-0000000009000fff (prio 0, i/o): pl011
    0000000040000000-000000007fffffff (prio 0, ram): mach-virt.ram

DMA: devices use dma_memory_read/write() or address_space_rw() on their DMA address space, which may sit behind an emulated IOMMU (SMMUv3 on ARM virt), so the same code works whether or not an IOMMU is present.

Why the layering matters: the same device model works with TCG and KVM, and board code can move, alias or overlay regions (e.g. a boot ROM that is remapped after boot) without touching the device code.