---
title: Virtualization and nested translation
heading: Hypervisors, VM exits, two-dimensional page walks, tagged TLBs and I/O virtualization
eyebrow: S4 · Lesson 25 · Virtualization and nested translation
xp: 50
---

::: hook
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.
:::

::: play
- 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, $(n + 1)(m + 1) - 1$.
- 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.
:::

```sim plot
view: graph
x: n = 1..6
y: A = 0..50
samples: 6
slider: m = 1..5 step 1 = 4
plot: (n + 1)*(m + 1) - 1 | stems | label: nested walk
plot: n | stems | dashed | label: native walk
```

::: 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?

- [ ] 8
- [ ] 16
- [x] 24
- [ ] 4

::: reveal
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: $4 \times 5 = 20$. The final guest-physical address of the data needs one more host walk: 4 more. In all $(4 + 1)(4 + 1) - 1 = 24$, six times a native walk.
:::
:::

::: nameit
## 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 $E\,t_\text{exit}$ 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
$$(n + 1)(m + 1) - 1$$
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.
:::

::: 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?

::: solution
1. **Native miss cost.**
   $$4 \times 20\ \text{ns} = 80\ \text{ns}$$
   which is 240 cycles at 3 GHz.
2. **Native CPI.**
   $$0.001 \times 240 = 0.24$$
3. **Nested CPI.** In a VM the walk takes 24 accesses, 480 ns, which is 1440 cycles:
   $$0.001 \times 1440 = 1.44$$
   six times as much.
4. **With 2 MB pages.** With 3 levels in each dimension the nested walk drops to
   $$(3 + 1)(3 + 1) - 1 = 15$$
   accesses, and the TLB misses less often too.

**Answer:** the walks add 0.24 CPI natively and 1.44 CPI in a VM.
:::
:::

::: 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?

::: solution
1. **Time per second.**
   $$50\,000 \times 1.5\ \text{µs} = 75\ \text{ms}$$
   each second, which is 7.5% of a core.
2. **Where it goes.** That time is spent saving and restoring state rather than running either the guest or useful hypervisor work.
3. **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.
:::
:::

::: example
## With 5-level page tables in both the guest and the host, how many accesses can a nested walk take?

::: solution
1. **Nested walk.**
   $$(5 + 1)(5 + 1) - 1 = 35$$
   accesses, against 5 natively.
2. **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
kind: parts
given: n = 3 | 4 | 5; m = 2 | 3 | 4

## A guest's page table has {n} levels; the host's nested table has {m}.

::: part
answer: (n + 1)*(m + 1) - 1
unit: accesses
tolerance: 0

### How many memory accesses can a TLB miss take?

::: misses
- [n*m] Each guest level's entry is read too, and the final address needs a host walk.
- [n + m] The host walk happens for every guest level.
- [any] $(n + 1)(m + 1) - 1$.
:::
:::

::: part
answer: ((n + 1)*(m + 1) - 1)/n
tolerance: 0.5%

### How many times a native {n}-level walk is that?

::: misses
- [any] Nested over native.
:::
:::
:::

::: check
kind: parts
given: E = 10000 | 50000 | 200000; t = 1 | 2

## A guest causes {E} VM exits a second, each costing {t} µs.

::: part
answer: E*t/1e6*100
unit: %
tolerance: 0.5%

### What fraction of a core do they use?

::: misses
- [any] Exits per second × cost.
:::
:::
:::

::: check
kind: parts
given: k = 0.5 | 1 | 3; a = 15 | 30; f = 2 | 3; n = 4; m = 4

## A program misses the TLB {k} times per thousand instructions; each page-walk access takes {a} ns on a {f} GHz core, with 4-level tables in both the guest and the host.

::: part
answer: k/1000*4*a*f
tolerance: 0.5%

### What CPI do native walks add?

::: misses
- [any] Misses per instruction × 4 accesses × time in cycles.
:::
:::

::: part
answer: k/1000*24*a*f
tolerance: 0.5%

### And nested walks?

::: misses
- [k/1000*16*a*f] Count the guest entries and the final host walk: 24 accesses.
- [any] 24 accesses per miss.
:::
:::
:::

::: check
kind: choice

## Why do TLB entries carry address-space and VM identifiers?

- [ ] To make each entry larger
- [x] So switching to another process or VM doesn't require flushing the TLB: entries from different contexts coexist, and each lookup matches only its own context's
- [ ] To prevent page faults
- [ ] To speed up the page walk

::: misses
- [1] The tag is the cost, not the goal.
- [3] Faults depend on the page tables, not the TLB's tags.
- [4] Tags help only by avoiding misses after a switch; the walk itself is unchanged.
:::
:::

::: check
kind: derivation
vars: n = 1..6, m = 1..6, c = 0..3

## 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
answer: n*(m + 1)

### How many accesses do the guest levels take, each with its host walk, without caching?
:::

::: step
answer: (n + 1)*(m + 1) - 1

### How many in all, adding the final host walk?
:::

::: step
answer: (n + 1)*(m - c + 1) - 1

### And with c host levels cached?
:::

::: right
Right. Caching the host's upper levels shortens every one of the n + 1 host walks.
:::
:::

::: check
kind: python

## 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 starter
def nested_walk(n, m, c=0):
    return n + m

def walk_cpi(misses_pk, refs, ns, ghz):
    return 0.0
```

```python solution
def nested_walk(n, m, c=0):
    host = m - c
    return n * (host + 1) + host

def walk_cpi(misses_pk, refs, ns, ghz):
    return misses_pk / 1000 * refs * ns * ghz
```

```python tests
assert nested_walk(4, 4) == 24 and nested_walk(5, 5) == 35, "4 × 4: 24; 5 × 5: 35."
assert nested_walk(3, 3) == 15, "Huge pages both ways: 15."
assert nested_walk(4, 4, 2) == 14, "Skipping two host levels: 14."
assert abs(walk_cpi(1, 24, 20, 3) - 1.44) < 1e-12, "The worked example: 1.44 CPI in a VM."
```

::: right
Right. Each cached host level saves n + 1 accesses, which is why nested translation caches pay off so well.
:::
:::

::: explain
## Why does a TLB miss cost so much more inside a virtual machine with nested paging?

::: model
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
::: check
given: n = 2 | 4; m = 3 | 5
answer: (n + 1)*(m + 1) - 1
unit: accesses
tolerance: 0

## Nested walk accesses, {n} guest and {m} host levels?

::: misses
- [any] $(n + 1)(m + 1) - 1$.
:::
:::

::: check
given: E = 20000 | 100000; t = 1.2 | 2.5
answer: E*t/1e6*100
unit: %
tolerance: 0.5%

## Core fraction for {E} exits a second at {t} µs?

::: misses
- [any] $Et$.
:::
:::

::: check
given: k = 2 | 5; a = 10 | 25
answer: k/1000*24*a*3
tolerance: 0.5%

## Nested walk CPI: {k} MPKI, {a} ns per access, 3 GHz, 4 × 4 levels?

::: misses
- [any] Misses × 24 × time in cycles.
:::
:::
:::
