The problem
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.
The classic trick: run the guest kernel in user mode, so privileged instructions trap to the hypervisor, which emulates them. Old x86 broke it: 17 sensitive instructions, such as POPF, misbehave or leak state in user mode without trapping.
VT-x: exits and entries
Intel VT-x adds a new pair of CPU modes. The hypervisor runs in root mode. Guests run in non-root mode, which has its own kernel and user levels, so the guest kernel truly runs in kernel mode.
The hypervisor fills the VMCS, a control block in memory: the guest's instruction pointer and control registers, its own state, and which events must cause an exit, such as port I/O. VMLAUNCH then enters the guest, and the mode becomes non-root.
In non-root mode the guest's instructions run directly on the real CPU, with no hypervisor in between. Registers al and bl both start at 2: the CPU adds them, then adds the code of the character '0', giving the character '4'.
Line 4 writes '4' to I/O port 0x3f8, a serial port. The VMCS says port I/O must exit, so the CPU saves the guest's state in the VMCS, switches to root mode and enters the hypervisor. Exit reason 30: I/O instruction.
On Linux, KVM is the hypervisor inside the kernel. Its KVM_RUN call returns to the VMM program with KVM_EXIT_IO: 1 byte out to port 0x3f8. The program acts as the serial port and prints 4 on the console.
The program calls KVM_RUN again. The CPU reloads the guest's state from the VMCS, switches back to non-root mode and goes on just past the out instruction, where KVM moved it. To the guest, the write simply worked.
Nested page tables
Memory needs the same care. The guest's own page table maps guest virtual to guest physical addresses, but guest physical is not real memory. A second table only the hypervisor controls, the EPT, maps guest physical to host physical.
The guest reads address 0x401234 and the TLB misses. The CPU finds page 0x401 in the guest's table: guest frame 0x2. Then it finds that frame in the EPT: host frame 0x7a3. The byte is at 0x7a3234. No exit.
The TLB keeps the whole result, page 0x401 to host frame 0x7a3, so the next read skips the walk. That matters: with 4 levels in each table, one full walk can take up to 24 memory reads instead of 4.
Before EPT, hypervisors kept shadow page tables and had to catch every change the guest made to its own. With VT-x and EPT, the guest runs its kernel and edits its page tables at full speed. The hypervisor steps in only on exits.
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.