04 Hwvirt Kvmqemu
04 Hwvirt Kvmqemu
Computing
Lecture 4: Hardware-assisted
CPU virtualization in KVM/QEMU
Mythili Vutukuru
IIT Bombay
Spring 2021
Hardware-assisted Virtualization
• Modern technique, after hardware support for virtualization
introduced in CPUs
• Original x86 CPUs did not support virtualization
• Intel VT-X or AMD-V support is widely available in modern systems
• Special CPU mode of operation called VMX mode for running VMs
• Many hypervisors use this H/W feature, e.g., QEMU/KVM in Linux
KVM (kernel module) When invoked, KVM switches to VMX mode to run guest
CPU with VMX mode CPU switches between VMX and non-VMX root modes
Libvirt and QEMU/KVM
• When you install QEMU/KVM on Linux, libvirt is also installed
• A set of tools manage hypervisors, including QEMU/KVM
• A daemon runs on the system and communicates with hypervisors
• Exposes an API using which hypervisors can be managed, VM created etc.
• Commandline tool (virsh) and GUI (virt-manager) use this API to manage VMs
• What is VMCS?
• Common memory area accessible in both modes VMCS
• One VMCS per VM (KVM tells CPU which VMCS to use)
• What is stored in VMCS?
• Host CPU context: Stored when launching VM, restored on VM exit
• Guest CPU context: Stored on VM exit, restored when VM is run
• Guest entry/execution/exit control area: KVM can configure guest memory
and CPU context, which instructions and events should cause VM to exit
• Exit information: Exit reason and any other exit-related information
• VMCS information (e.g., exit reason) exchanged with QEMU via
kvm_run structure
• VMCS only accessible to KVM in kernel mode, not to QEMU userspace
VMX mode execution
• How is guest OS execution in VMX mode different?
• Restrictions on guest OS execution, configurable exits to KVM
• Guest OS exits to KVM on certain instructions (e.g., I/O device access)
• No hardware access to guest, emulated by KVM
• Guest OS usually exits on interrupts (interrupts handled by KVM, assigned to
the appropriate host or guest OS)
• KVM can inject virtual interrupts to guest OS during VMX mode entry
• All of the above controlled by KVM via VMCS
• Mimics the trap-and-emulate architecture with hardware support
• Guest runs in a (special) ring 0, but trap-and-emulate achieved
QEMU/KVM operation revisited
Root mode VMX mode
Guest
Guest VM physical memory application
VCPU-0 kvm_run