On this page
KVM Memory and Device Virtualization
Coverage: EPT/NPT (stage-2 page table) → MMU virtualization → virtio (paravirtualization) → VFIO (passthrough) → vhost (kernel-space acceleration) → virtio-net/virtio-blk detailed explanation Kernel version: 2.6 ~ 6.x
Memory Virtualization: EPT/NPT
Three-level address translation:
GVA (guest virtual address)
→ guest page table (managed by guest OS)
→ GPA (guest physical address)
→ EPT/NPT (managed by KVM, "stage-2 page table")
→ HPA (host physical address)
EPT (Intel Extended Page Tables) / NPT (AMD Nested Page Tables):
Hardware directly executes the 2D page walk for GVA→GPA→HPA (24 steps!)
→ No need for KVM to maintain shadow page tables
→ Supported by hardware on modern CPUs
EPT violation: guest modifies its own page table → EPT page table becomes stale → VM exit
→ KVM updates EPT mapping → VMRESUME
MMU Virtualization
// arch/x86/kvm/mmu/mmu.c
// KVM MMU maintains two types of page tables:
// 1. EPT page table (used by hardware): GPN → HPN
// 2. Shadow page table (only for older CPUs without EPT): GVA → HPA
// TDP (Two-Dimensional Paging) = EPT on Intel, NPT on AMD
// kvm_mmu_page_fault():
// guest accesses GPA → EPT violation → handle_ept_violation()
// → kvm_mmu_map() → establishes GPA→HPA mapping in EPT
// → returns to guest
virtio: Paravirtualized I/O
// drivers/virtio/
// Design principle: guest knows it is in a VM → communicates using shared memory (virtqueue)
// No need to emulate real hardware → extremely high I/O performance
// virtio device model:
// Frontend (guest): virtio-blk, virtio-net, virtio-scsi, virtio-gpu
// Backend (host):
// QEMU (software emulation)
// vhost (kernel-space acceleration: vhost-net, vhost-scsi, vhost-vsock)
// vDPA (hardware acceleration: NICs/smartNICs)
virtqueue: Shared Ring Buffer
// Core transmission mechanism of virtio:
// 1. split virtqueue: legacy format (descriptor table + available ring + used ring)
// 2. packed virtqueue (1.1): more compact, cache-line friendly
// guest submits I/O:
// write descriptor + available ring → kick (write doorbell)
// host consumes:
// fetch descriptor → process I/O → write used ring → interrupt (MSI-X)
// guest completes:
// interrupt → read used ring → free buffer
vhost: Kernel-Space Acceleration
// drivers/vhost/net.c
// vhost moves the virtio data path to the kernel:
// guest → virtqueue → vhost (host kernel) → tap device → protocol stack
// Bypasses QEMU user space → latency reduced by ~50%
// vhost-net: networking (most commonly used)
// vhost-scsi: storage
// vhost-vsock: guest↔host communication (AF_UNIX across VM boundaries)
VFIO: Device Passthrough
// Exclusively assign a physical PCIe device to a guest:
// 1. Unbind device from host (echo dev > /sys/bus/pci/drivers/xxx/unbind)
// 2. Bind to vfio-pci (echo vendor device > /sys/bus/pci/drivers/vfio-pci/new_id)
// 3. QEMU: -device vfio-pci,host=01:00.0
// 4. IOMMU isolation: device DMA can only access this VM's memory
// Performance: close to bare metal (GPU, NVMe passthrough to guest)
// Limitations: Cannot live migrate (device state cannot be saved/restored)
References
- Source Code:
arch/x86/kvm/mmu/,drivers/virtio/,drivers/vhost/,drivers/vfio/ - Kernel Documentation:
Documentation/virt/kvm/,Documentation/driver-api/virtio/ - LWN: "Virtio without the QEMU middleman (vhost)", "VFIO and device assignment"
Keywords: EPT, NPT, stage-2 page table, virtio, virtqueue, vhost, VFIO, PCI passthrough