HYPERVISORVMCSEPT gPA→hPA2→7a3$ ./vmm4HOSTHOST ONLY1mov $0x3f8, %dx2add %bl, %al3add $'0', %al4out %al, (%dx)CPUVT-x MODErootTLB page→frameguest page table401→2popf4440123422347a3234401→7a3
Back: the host side (hypervisor, VMCS, EPT, console)Front: the guest side (guest code, guest page table) and the CPU with its mode and TLBTokens: a VM exit and entry through the gate; an address walking both page tablesThe host side at the back: the hypervisor, the VMCS, the EPT and the console. A wall with one gate, VM exit and VM entry. The guest side in front: the guest kernel's code, its page table, and the real CPU with its VT-x mode and its TLB. Guest instructions run straight on the CPU. The out instruction exits through the gate into the VMCS and flips the mode to root; the hypervisor prints 4 and VM entry flips it back. An address then walks the guest page table and, over the shut gate, the EPT; the TLB keeps the answer. VT-x is Intel's name; AMD-V (SVM) works the same way with different instruction names (VMRUN, #VMEXIT) and a VMCB instead of a VMCS. Real VMCS fields are read and written with special instructions (VMREAD, VMWRITE), not as plain memory.

The problem

  1. A virtual machine runs a whole guest operating system. Its kernel expects kernel mode and full control of the CPU and memory. But the hypervisor, the software that runs virtual machines, must keep the real control.

VT-x: exits and entries

Nested page tables

What happens

;; step 1/12 · guest-kernel
guest kernel   thinks it owns the CPU, memory and devices
hypervisor     really owns them; shares them between guests

the wish:  guest code runs directly on the real CPU (fast)
           but every risky action reaches the hypervisor

The guest code is the example from LWN's "Using the KVM API": it runs in 16-bit real mode with rax = 2 and rbx = 2, prints 4 and a newline on the serial port and stops with hlt. The card shows its first four instructions. The addresses in part 2 are made up for the example, and the tables are drawn as one lookup each. Real x86-64 tables have 4 levels in the guest and 4 in the EPT; page walk caches and the TLB keep the 24-read worst case rare.