On this page
KVM Architecture
Coverage: KVM as Linux hypervisor → VMX/SVM hardware virtualization → vCPU lifecycle → VM exit handling → KVM + QEMU collaboration model → /dev/kvm ioctl API Kernel version: 2.6.20 ~ 6.x
Overview
KVM (Kernel-based Virtual Machine) transforms the Linux kernel into a type-1 hypervisor—leveraging Intel VMX or AMD SVM hardware virtualization extensions to allow guests to run directly on physical CPUs in VMX non-root mode. Each VM is a QEMU process in user space, and each vCPU is a thread within that process.
Architecture Layers
VM Entry / VM Exit
vCPU Thread Model
// Each vCPU = one QEMU thread, executing a loop:
while
Scheduler Interaction
// KVM knows when a vCPU is preempted via preempt notifier:
// → kvm_sched_out(): vCPU is scheduled out
// → kvm_sched_in(): vCPU is scheduled in
// Used for: steal time accounting, TSC offset calculation
/dev/kvm Interface
// Create VM:
int kvm_fd = ;
int vm_fd = ;
// Register memory (guest physical address space):
struct kvm_userspace_memory_region region = ;
;
// Create vCPU:
int vcpu_fd = ;
// Get KVM version/capabilities:
int version = ;
struct kvm_cpuid2 cpuid = ;
;
Interrupt Injection
// KVM injects interrupts to the guest:
// 1. Set interrupt info fields in VMCS (VMX) or VMCB (SVM)
// 2. Set interrupt window → guest responds immediately after VM entry
// 3. If vCPU is not running (scheduled out) → set kick flags
// Interrupt sources:
// - virtio devices (msix)
// - Emulated devices (8259A PIC, IOAPIC)
// - IPI (multiple vCPUs within guest)
// - Timer (hrtimer → KVM timer)
References
- Source Code:
virt/kvm/kvm_main.c(core),arch/x86/kvm/vmx/vmx.c(Intel VMX),arch/x86/kvm/svm/svm.c(AMD SVM) - Kernel Documentation:
Documentation/virt/kvm/ - LWN: "KVM: the Linux virtual machine monitor"
Keywords: KVM, VMX, SVM, vCPU, VM exit, VMLAUNCH, VMRESUME, /dev/kvm, KVM_RUN, interrupt injection