Open-Source Wikis

/

Linux

/

Subsystems

/

Virtualization

torvalds/linux

Virtualization

Purpose

virt/ holds the architecture-independent host-side code for KVM (Kernel-based Virtual Machine) and a few related subsystems. Architecture-specific KVM code lives under each arch/<arch>/kvm/. Together these form the host hypervisor that QEMU and other VMMs program.

User-space access to host pass-through devices is the job of VFIO, which lives in drivers/vfio/ (cross-referenced here).

Directory layout

virt/
├── kvm/           # Architecture-independent KVM
├── lib/           # Helpers shared across virt code
└── coco/          # Confidential computing helpers (TDX / SEV bounce buffer policy)

The bulk of KVM code is split:

  • virt/kvm/ — common: vcpu lifetime, the dirty-ring, eventfd integration, IRQ routing, MMU notifier glue, debugfs.
  • arch/x86/kvm/ — Intel VT-x (vmx/) and AMD-V (svm/).
  • arch/arm64/kvm/ — KVM/arm64.
  • arch/riscv/kvm/, arch/powerpc/kvm/, arch/s390/kvm/, arch/loongarch/kvm/ — others.

Key abstractions

Symbol File Purpose
struct kvm include/linux/kvm_host.h A virtual machine.
struct kvm_vcpu same A virtual CPU.
struct kvm_memory_slot same A guest memory region.
struct kvm_io_bus same The MMIO/PIO dispatch fabric.
KVM_RUN ioctl virt/kvm/kvm_main.c Run a vcpu until it exits.
MMU notifiers virt/kvm/kvm_main.c, mmu_notifier.h Keep guest shadow/EPT in sync with the host mm.

How it works

graph LR
    QEMU[QEMU / VMM] -->|"ioctl /dev/kvm"| KVM[virt/kvm + arch/*/kvm]
    KVM -->|VM-enter| GUEST[Guest VM]
    GUEST -->|VM-exit on MMIO/PIO/HLT/CPUID/...| KVM
    KVM -->|emulate or inject| QEMU
    KVM -->|"page fault: host mm"| MM[mm/ page tables]
    MM -->|notifier| KVM
    KVM -->|virtio-net / vhost| VHOST[drivers/vhost/]
    VHOST --> NET[Networking]
    KVM -->|virtio-blk / vhost| VHOST_BLK[drivers/vhost/]
    KVM -->|VFIO pass-through| VFIO[drivers/vfio/]

A user-space VMM opens /dev/kvm, creates a VM (KVM_CREATE_VM), creates vcpus (KVM_CREATE_VCPU), maps guest memory (KVM_SET_USER_MEMORY_REGION), and then calls KVM_RUN repeatedly. The kernel runs the guest until a VM-exit (MMIO, PIO, halt, CPUID, etc.); for "exits the kernel can't handle" it returns to user space which emulates and resumes.

MMU

KVM keeps a shadow page table or hardware-assisted second-stage translation (EPT/NPT, Stage-2). MMU notifiers keep this synchronized with the host's view: when the host swaps or migrates a page, the corresponding guest mapping is invalidated. All architectures share the framework; the implementation is per-arch.

eventfd integration

KVM has tight integration with eventfd, used for in-kernel IRQ injection (KVM_IRQFD) and notify-from-kernel (KVM_IOEVENTFD). This is what makes vhost-net deliver packets to the guest without a user-space round trip.

Confidential computing

virt/coco/ handles policy for confidential-computing technologies (Intel TDX, AMD SEV-SNP) where memory is encrypted and the host has limited visibility.

Dirty page tracking

The dirty bitmap (KVM_GET_DIRTY_LOG) and the newer dirty ring (KVM_DIRTY_LOG_RING) let live migration software efficiently track guest writes.

VFIO and IOMMUFD (pass-through)

For PCI device pass-through, drivers/vfio/ exposes the device safely to user space. The newer iommufd subsystem (also in drivers/iommu/iommufd/) is the modern replacement for VFIO's container/group model.

vhost

drivers/vhost/ is a kernel-side virtio backend. Used by QEMU's vhost-net, vhost-scsi, and vhost-user setups. Sits next to KVM in the data path.

Integration points

  • arch//kvm/: every arch with KVM has its own.
  • mm/: MMU notifiers, memory slots.
  • drivers/vfio/, drivers/iommu/: device pass-through.
  • drivers/vhost/: virtio backends.
  • kernel/sched/: vcpu threads are normal kernel tasks; sched_yield/yield_to is used for vcpu hint.
  • net/: vhost-net feeds packets.
  • virt/coco/: TDX/SEV.

Key source files

File Purpose
virt/kvm/kvm_main.c The KVM core: ioctls, vcpu loop, memory slots.
virt/kvm/eventfd.c irqfd / ioeventfd.
virt/kvm/dirty_ring.c Dirty ring.
virt/kvm/coalesced_mmio.c Batched MMIO.
arch/x86/kvm/x86.c x86 KVM core.
arch/arm64/kvm/ KVM/arm64.

Entry points for modification

  • New ioctl: extend kvm_arch_vm_ioctl or kvm_arch_vcpu_ioctl and the UAPI header.
  • New optimization: most happen per-arch; coordinate with kvm@vger.kernel.org.
  • New device emulation: usually goes in user space (QEMU) unless it must be in-kernel for performance (e.g. vhost-net).

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Virtualization – Linux wiki | Factory