KVM
Also called Kernel-based Virtual Machine
KVM is a module of the Linux kernel that turns Linux into a hypervisor, using the hardware virtualization features of the processor to run guests. It handles only the work that needs those features and leaves guest setup and device emulation to a separate program. The split keeps the privileged part small and lets different programs share the same kernel support. KVM ships in the mainline kernel, so nearly any Linux host with a suitable processor can run guests.
That program, the virtual machine monitor, runs as an ordinary process on the host and talks to KVM through a device file. It creates each guest and gives it memory and virtual processors. Each guest is set up with a series of requests on that file, which return new handles for the guest and for each of its processors. Any program with access to the file can run guests, so access to it is limited.
Because KVM is part of Linux, a guest is scheduled and limited like any other process, so host tools such as cgroups work on it too. Several of the largest public clouds run their virtual machines on KVM or on a hypervisor derived from it. Google Cloud runs on KVM, and the Nitro hypervisor of AWS is built on it. Ending a guest is as simple as killing its monitor process.
Run loop
The run loop is the cycle in which a thread of the monitor runs one vCPU through KVM. The thread asks KVM to run the vCPU, and KVM enters the guest until the guest does something that needs the host. Each vCPU has its own thread, so a guest with several vCPUs can run on several host cores at once when they are free. Host tools report the time spent inside the guest as guest time for that thread.
When KVM can handle an exit itself, such as a timer or an interrupt controller access, it does so in the kernel and enters the guest again. These exits never leave the kernel, which keeps them cheap. KVM emulates the interrupt controller and timers in the kernel for this reason, since guests touch them constantly. Moving that emulation into the kernel trades a little more privileged code for far fewer trips to user space.
When the guest touches an emulated device, the call returns to the monitor in user space. The monitor emulates the device and calls KVM again, and the loop starts over. That trip through user space is the slowest kind of exit. Some device work avoids it through vhost, which moves the data path of virtio devices from the monitor into the kernel.
Guest memory
Guest memory is ordinary memory of the monitor process, mapped into the guest by KVM. The guest treats it as physical memory, while the host sees pages of a normal program. The monitor can map it from shared memory, which lets another process, such as a device backend, read it directly. The monitor itself can read and write guest memory at any time, which is how it moves data for emulated devices.
The Linux kernel can manage those pages like the memory of any other program, swapping them out or backing them with huge pages. The guest does not notice, because its own page tables stay the same. The host can also give guests more memory than it has and rely on them not using all of it. A balloon driver in the guest can hand pages back when the host runs short.
The kernel can also merge identical pages across guests and keep one shared copy. This saves memory when many guests run the same operating system and hold the same files in memory. Merging has a cost, because a scanner has to compare pages in the background. Shared pages can also leak information between guests through timing, so platforms often turn merging off for untrusted tenants.
User-space monitors
User-space monitors are the programs that run on top of KVM, one process for each guest. KVM stays the same underneath, so the monitor decides how fast a guest boots and how much device code it can reach. One host can run different monitors side by side, each with its own guests. Monitors differ mainly in the devices they offer and the language they are written in.
QEMU is the oldest and most complete monitor, with emulation for a long list of devices and machine types. It can run almost any guest operating system, at the cost of a large body of code. It can also run without KVM by translating guest code in software, which is slower but works across processor architectures. Most desktop and server virtualization on Linux, through tools such as libvirt, runs on QEMU.
Newer monitors written in Rust use KVM with only a few virtio devices. This is the usual way to build a microVM, trading broad hardware support for faster boots and less code for a guest to attack. Their devices pass data through queues in shared memory rather than imitating real hardware. A guest kernel with virtio drivers needs nothing more to boot on such a monitor.
| Monitor | Description |
|---|---|
| QEMU | QEMU is a general-purpose monitor that emulates many devices and machine types. |
| Firecracker | Firecracker is a minimal monitor written in Rust to run microVMs. |
| Cloud Hypervisor | Cloud Hypervisor is a Rust monitor with a slightly larger device model than Firecracker. |
| crosvm | The crosvm monitor is a Rust monitor with a small device model. |