Document navigation

CPU Virtualization

In the x86 architecture, CPUs generally have four privilege levels (RING0 to RING3) for operating systems and applications to access hardware. Linux uses only two of these levels: RING0 (kernel mode) and RING3 (user mode).

VMX Root Mode and VMX Non-Root Mode: For hardware-assisted virtualization, CPUs introduce two operation modes to enable virtual machines to run without modifying the operating system: VMX root mode and VMX non-root mode. The host runs in root mode, with its kernel in RING0 and user-mode programs in RING3. The virtual machine runs in non-root mode, with its kernel in RING0 and user-mode programs in RING3.

VM Exit and VM Entry: A switch from non-root mode to root mode occurs when a VM in non-root mode encounters an external interrupt or page fault, or executes the VMCALL instruction to invoke VMM services. This transition is called VM Exit. Conversely, when the VMM executes the VMLAUNCH or VMRESUME instruction to switch to non-root mode, the hardware loads the VM context and resumes VM instruction execution. This transition is called VM Entry.

See Figure 1 for an illustration.
Figure 1. Non-Root Mode and Root Mode


After a virtual machine triggers a VM Exit from non-root mode to root mode, KVM takes further action based on the exit reason. If the exit is due to an I/O operation, KVM delegates the processing to QEMU. For non-I/O exits, KVM handles them directly. After processing, KVM initiates a VM Entry to switch back to non-root mode for virtual machine execution.

See Figure 2 for an illustration.
Figure 2. Mode Switching