S4 · Lesson 25 · Virtualization and nested translation
Hypervisors, VM exits, two-dimensional page walks, tagged TLBs and I/O virtualization
Most server software now runs inside virtual machines, each believing it owns a whole computer. The hypervisor beneath them must intercept what a guest isn't allowed to do, translate the guest's idea of physical memory into real memory, and share devices, all without slowing the guest much. Hardware support made that practical, but at a price that architecture explains: a TLB miss inside a VM can take six times as many memory accesses as one on bare metal, and every trap into the hypervisor costs microseconds.
- The stems count the memory accesses a TLB miss can take with nested paging: a guest page table of n levels, each of whose pointers is a guest-physical address the host must translate with its own m-level table, .
- The dashed stems are a native walk of n levels.
- With four levels each, a native walk takes 4 accesses and a nested one 24; huge pages and page-walk caches attack both factors.
Predict
A guest uses 4-level page tables, and the hypervisor's nested tables have 4 levels too. In the worst case, how many memory accesses does a TLB miss take?
Each of the guest's 4 page-table pointers is a guest-physical address the host must translate with a 4-level walk, plus the guest entry itself: . The final guest-physical address of the data needs one more host walk: 4 more. In all , six times a native walk.
Name it
The hypervisor
A hypervisor (virtual machine monitor) runs guests in a less privileged mode. Sensitive operations, such as writing control registers or touching devices, cause a VM exit: the hardware saves the guest's state and runs the hypervisor, which emulates the operation and resumes the guest. Each exit costs about a microsecond or more, so a guest exiting E times a second loses about of a core.
Two-dimensional translation
With nested paging (Intel EPT, AMD NPT, Arm stage 2), the guest's page tables map guest-virtual to guest-physical addresses, and the host's tables map guest-physical to host-physical. A walk of n guest levels and m host levels can take
memory accesses. Page-walk caches skip upper levels, and huge pages remove levels from either dimension. The older alternative, shadow page tables kept by the hypervisor, gives native-length walks but makes every guest page-table update an exit.
Tagged TLBs
TLB entries carry an address-space ID and a VM ID, so switching between processes or VMs doesn't flush the TLB.
I/O
An IOMMU translates device DMA addresses so a device can be handed directly to a guest; SR-IOV splits one physical device (a network card) into many virtual functions, one per VM, avoiding the hypervisor on the data path.
Worked example
A page walk's memory accesses average 20 ns, and a program misses the TLB once per thousand instructions on a 3 GHz core. What CPI do the walks add natively and in a VM, with 4-level tables in both dimensions?
Show the solution
- Native miss cost. which is 240 cycles at 3 GHz.
- Native CPI.
- Nested CPI. In a VM the walk takes 24 accesses, 480 ns, which is 1440 cycles: six times as much.
- With 2 MB pages. With 3 levels in each dimension the nested walk drops to accesses, and the TLB misses less often too.
Answer the walks add 0.24 CPI natively and 1.44 CPI in a VM.
Worked example
A guest's network driver causes 50 000 VM exits a second, each costing 1.5 µs. What fraction of a core goes to them?
Show the solution
- Time per second. each second, which is 7.5% of a core.
- Where it goes. That time is spent saving and restoring state rather than running either the guest or useful hypervisor work.
- The fix. Paravirtual drivers that batch requests, or SR-IOV that bypasses the hypervisor, cut it to near zero.
Answer 7.5% of a core goes to VM exits.
Worked example
With 5-level page tables in both the guest and the host, how many accesses can a nested walk take?
Show the solution
- Nested walk. accesses, against 5 natively.
- What that means. The cost of a larger virtual address space grows quadratically under virtualization, which is one reason processors cache intermediate nested translations aggressively.
Answer a nested walk can take 35 memory accesses.
Check
A guest's page table has 4 levels; the host's nested table has 2.
(a)
How many memory accesses can a TLB miss take?
(b)
How many times a native 4-level walk is that?
Check
A guest causes 50000 VM exits a second, each costing 1 µs.
(a)
What fraction of a core do they use?
Check
A program misses the TLB 1 times per thousand instructions; each page-walk access takes 15 ns on a 3 GHz core, with 4-level tables in both the guest and the host.
(a)
What CPI do native walks add?
(b)
And nested walks?
Check
Why do TLB entries carry address-space and VM identifiers?
Check
A guest walk has n levels and each host walk m levels; page-walk caches skip c upper levels of every host walk (c < m).
Step 1
How many accesses do the guest levels take, each with its host walk, without caching?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How many in all, adding the final host walk?
Step 3
And with c host levels cached?
Check
Write nested_walk(n, m, c=0): the memory accesses of a nested page walk with n guest levels and m host levels, when page-walk caches let every host walk skip c levels; and walk_cpi(misses_pk, refs, ns, ghz): the CPI added by walks of refs accesses at ns each.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does a TLB miss cost so much more inside a virtual machine with nested paging?
Inside a VM, the guest's page tables live at guest-physical addresses, which are not real memory addresses: each one must itself be translated by the host's page tables before the guest's walk can read it. So every step of the guest's walk is preceded by a complete walk of the host's tables, and once the guest's walk produces the final guest-physical address of the data, that too needs a host walk. With n guest levels and m host levels, the total grows as their product, (n + 1)(m + 1) − 1, instead of n. Huge pages, which remove levels in either dimension, and caches for intermediate translations bring it back down.
Practice
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.