Linux Kernel Virtualization Explained: How KVM Works

This article provides an overview of how the Linux operating system natively supports virtualization using the Kernel-based Virtual Machine (KVM). By turning the standard Linux kernel into a Type-1 hypervisor, KVM allows the system to run multiple isolated virtual machines directly on bare-metal hardware. You will learn how KVM interacts with hardware-assisted virtualization extensions, manages memory and processing tasks using native kernel subsystems, and interfaces with user-space software to manage virtualized environments.

The Architecture of KVM

Kernel-based Virtual Machine (KVM) is an in-tree module within the Linux kernel (specifically, kvm.ko, alongside processor-specific modules like kvm-intel.ko and kvm-amd.ko). When loaded, KVM extends the core kernel to act as a hypervisor. Rather than running an entirely separate hypervisor layer beneath the operating system, Linux itself becomes the hypervisor, retaining full access to all host hardware, scheduling capabilities, and device drivers.

Hardware-Assisted CPU Virtualization

KVM relies fundamentally on hardware virtualization extensions provided by modern processors, such as Intel VT-x and AMD-V. These hardware extensions introduce a dual-operating mode structure:

By offloading the execution of unmodified guest code directly to the physical CPU, KVM achieves near-native computational performance.

Virtual Machines as Standard Linux Processes

One of the defining strengths of KVM is that it does not implement a bespoke scheduler for virtual machines. Instead:

Because vCPUs are regular threads, they are scheduled by the standard Completely Fair Scheduler (CFS) or the newer EEVDF scheduler. This means VMs immediately benefit from kernel features such as CPU affinity (pinning vCPUs to specific physical cores), control groups (cgroups) for resource limiting and prioritization, and NUMA (Non-Uniform Memory Access) balancing.

Memory Virtualization and Management

Guest operating systems require their own physical memory address space. KVM coordinates with the CPU’s hardware memory management unit (MMU) through technologies like Intel Extended Page Tables (EPT) or AMD Nested Page Tables (NPT) to map Guest Physical Addresses (GPA) directly to Host Physical Addresses (HPA).

Furthermore, because VM memory allocations are backed by host virtual memory, KVM leverages core Linux memory management capabilities, including:

The Division of Labor: KVM and User Space

KVM focuses strictly on managing the CPU and memory. It exposes a character device interface at /dev/kvm, which user-space programs use to create, configure, and run virtual machines via standard ioctl() system calls.

A user-space component, most commonly QEMU, handles the rest of the virtualization stack:

  1. Device Emulation: Emulating storage controllers, network cards, graphics adapters, and serial ports.
  2. I/O Handling: When a guest attempts I/O operations, KVM traps the request and passes control back to user space (QEMU) to process the I/O.
  3. Paravirtualization (Virtio): For optimized performance, KVM and user-space tools support virtio drivers, which bypass legacy device emulation entirely. Virtio provides efficient, cooperative communication channels between the guest and the host for disk and network operations.

By integrating directly into the kernel's existing scheduler, memory manager, and device framework, KVM provides enterprise-grade virtualization without the complexity of maintaining a distinct operating system kernel.