Module 11 · Lesson 17 of 107

EPT, NPT and stage-2 hardware paging

Trace the hardware walks used by EPT, NPT, Arm stage 2, and RISC-V G-stage, then recover a host physical address from an EPT dump.

  • Beginner to advanced
  • 12 min read
  • 15 min lab
What you will need

Ubuntu 24.04 in WSL2 or a VM, a text editor, and the EPT debug snapshot in the lesson

Your place in the module
By the end you can 4 objectives
  • Trace a guest memory access through both translation stages.
  • Decode a four-level EPT walk, including its root, permissions, and leaf offset.
  • Identify the registers and address types used by AMD, Arm, and RISC-V.
  • Recover a GPA-to-HPA mapping from a VMM-side EPT snapshot.
Video coming soonEPT, NPT and stage-2 hardware paging
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from Shadow page tables and their cost
  • The VMM composes the guest and host mappings into a GVA-to-HPA page table that the processor can walk directly.
  • In synchronous shadow paging, writes to protected guest page-table pages trap so the VMM can update or invalidate dependent shadow entries.
  • A first access that lacks a usable shadow entry can exit even when the guest mapping already permits the access.
01

How hardware walks both x86 translations

5 min read

Shadow paging's cost pushed the industry to hardware. This lesson answers how EPT, NPT and stage-2/G-stage tables let hardware do the second walk.

KVM's x86 MMU documentation describes the job in terms of the addresses the processor must translate. Consider a guest kernel whose load uses a virtual address covered by a valid guest page table. The load can still fail before reaching its data because the processor first needs to read that page table. If the hypervisor has yet to map the guest physical page containing the table, hardware encounters a second-stage fault during the table read itself. This is the practical reason to distinguish the address of a guest page-table entry from the address that the entry ultimately helps translate.

Intel VT-x calls its hardware mechanism Extended Page Tables, or EPT. AMD calls its mechanism Nested Page Tables, or NPT. With guest paging enabled, the processor uses the guest's CR3 and the guest paging mode for the guest virtual address (GVA) to guest physical address (GPA) translation. The guest CR3 supplies a root in guest physical memory. The processor uses a separate root to translate GPA to host physical address (HPA). On Intel that root comes from the EPT pointer, EPTP, in the VMCS. On AMD it comes from nCR3 in the VMCB. The second-stage root identifies memory in the host physical address space.

Follow the operations for a load that needs a hardware walk. The processor forms the GPA of a guest page-table entry from the guest root and an index from the GVA. It translates that entry's GPA through EPT or NPT, then reads the guest entry from the resulting HPA. If the guest entry points to another table, the pointer in it is again a GPA. The processor repeats the operation for the next guest entry. Once the guest leaf supplies a guest physical base, the processor includes the appropriate offset from the GVA to obtain the final GPA. A second-stage translation of that GPA supplies the HPA for the load. Hardware can also need to update guest page-table bookkeeping bits, so the permissions for those implicit accesses matter.

One load, two translationsStart atguest CR3Form entryGPATranslateentry GPAUse EPTP ornCR3Read guestentryRepeatuntil guestleafForm dataGPATranslatedata GPAUse EPTP ornCR3Read dataat HPAStart atguest CR3Form entryGPATranslateentry GPAUse EPTP ornCR3Read guestentryRepeatuntil guestleafForm dataGPATranslatedata GPAUse EPTP ornCR3Read dataat HPA
The processor translates guest page-table addresses before it can use the guest leaf

The two stages each enforce access permissions. For example, an ordinary guest store can pass the guest's page permissions and still be denied by EPT's write permission. Conversely, a writable EPT mapping cannot grant a store that the guest's own paging rules forbid. When reading a dump, ask which memory access hardware was performing. A second-stage fault on a guest table page concerns that table access. A second-stage fault on the GPA produced by the guest leaf concerns the original instruction's access.

In the four-level EPT configuration, the second-stage walk traverses PML4, PDPT, PD, and PT, in that order, when it reaches a 4 KiB leaf. The GPA supplies bits 47:39, 38:30, 29:21, and 20:12 as the four indices. Each index selects an eight-byte entry. The entry address is the current table's physical base plus eight times the index. A valid non-leaf entry contains the host physical base of the next table. At the PT, a valid leaf supplies the host physical base of the mapped 4 KiB page. GPA bits 11:0 supply the byte offset. The offsets used to locate the entries and the offset into the final data page serve different purposes.

The EPTP packs configuration with the root address. Bits 2:0 select the memory type used for EPT paging-structure accesses. The supported choices include uncacheable (0) and write-back (6), subject to processor capabilities. Bits 5:3 encode the walk length minus one, so 3 describes the four-level walk. Bit 6 enables EPT accessed and dirty flags when the processor supports them. The root base occupies bits 51:12 of the format, subject to the processor's physical-address width, and the root is aligned to 4 KiB. To begin decoding a dump, extract that address field and confirm the configured walk length before splitting up the GPA. AMD's NPT entries use AMD's paging format, so use the AMD field layout for an nCR3 walk.

In a 4 KiB EPT leaf, bits 0, 1, and 2 encode read, write, and execute permissions in the basic format. A permitted access also needs the applicable permission at every preceding level. Leaf bits 5:3 encode the memory type for the mapped page. When EPT accessed and dirty tracking is enabled, bit 8 records access to an EPT entry used in translation and leaf bit 9 records a write to the mapped page. These are records of activity. Write permission describes whether a write is allowed, while the dirty flag describes a write that hardware has recorded. A dump can therefore describe a writable leaf whose dirty bit is clear.

02

How Arm stage 2 and RISC-V G-stage fit the walk

3 min read

On Arm AArch64, the guest's stage 1 translates a guest virtual address to an . IPA is Arm's term for the address x86 virtualization calls a GPA. In the usual guest EL1 translation regime, guest registers TTBR0_EL1 and TTBR1_EL1 hold the stage-1 table bases for the applicable virtual address ranges. When stage 2 is enabled for that guest regime, the processor translates the IPA to a physical address (PA) using the stage-2 tables controlled by EL2.

VTTBR_EL2 carries the stage-2 table base and the virtual machine identifier, or VMID. VTCR_EL2 supplies configuration including the translation granule, input address size, and starting level. The walker uses that configuration to select the IPA bits for each lookup. It starts at the physical stage-2 root, reads a descriptor, and follows a table descriptor to another physical table or finishes at a valid block or page descriptor. The leaf's output base and the byte offset from the IPA give the PA. A dump with only VTTBR_EL2 and the IPA is insufficient for a manual walk because the configuration determines how to interpret the hierarchy.

Now consider the read of an Arm stage-1 descriptor. Its address is in the guest's physical address space, so a stage-2 walk can be needed to fetch it. Hardware can report a stage-2 fault during that fetch. The final IPA produced by stage 1 also needs stage-2 translation for the requested load, store, or instruction fetch. This is the same dependency seen on x86. In both cases the second-stage tables themselves are reachable using physical addresses, allowing hardware to read those tables directly.

On RISC-V with the H extension, the guest's VS-stage translates a guest virtual address to a guest physical address. vsatp controls that stage and gives its root when guest paging is enabled. G-stage then translates the guest physical address to a supervisor physical address. The hypervisor configures hgatp, whose fields include the G-stage mode, the VMID, and the physical page number of the G-stage root. As on Arm and x86, G-stage applies to the guest physical addresses used for VS-stage table accesses as well as to the GPA for the requested access.

For a concrete RISC-V configuration, consider Sv39x4 in hgatp.MODE. It translates a 41-bit guest physical address and uses a 16 KiB root table aligned to 16 KiB. The top index is 11 bits wide. The lower table indices are nine bits wide and the page offset is 12 bits wide for a 4 KiB leaf. The widened root accommodates the extra two input address bits. If hgatp.MODE selects Bare, the G-stage address translation is bypassed. When inspecting a RISC-V dump, decode the mode before deriving indices from the GPA.

Translation controls by architecturex86Arm AArch64RISC-V HGuest rootcontrolCR3TTBR0_EL1 orTTBR1_EL1vsatpIntermediateaddressGPAIPAGPASecond-stageroot controlEPTP or nCR3VTTBR_EL2hgatpPhysical outputHost physicaladdressPhysicaladdressSupervisorphysicaladdress
Each architecture has guest controls for the first stage and hypervisor controls for the second

The register values identify where translation begins, and the active format tells you how to follow the entries. Across the three architectures you can use the same reasoning. Identify the input address to the second stage, decode its root and configuration, read entries in the physical address space, and take the offset width from the leaf that ends the walk. Use each architecture's own rules for descriptor validity, permissions, and address width.

03

Reading a second-stage mapping from the host

5 min read

A VMM-side dump can answer a precise question about a guest load. Given a GPA and a snapshot of the active second-stage tables, which physical page supplies the bytes, and does the table path permit the access? On Linux with KVM, userspace describes guest memory to the kernel. The KVM MMU installs, updates, and invalidates the hardware mappings as needed. The userspace guest memory registration supplies the GPA range and its userspace backing address. To see the hardware mapping, use a snapshot from the hypervisor side that includes the active root and its entries. The userspace backing address alone gives you a host virtual address for the memory, so it answers a different question.

The following is a symbolic EPT snapshot for the practice pack. It represents an already decoded four-level configuration, with valid entries, and uses labels for the field values. Each T and F label represents a host physical base aligned to 4 KiB. The input GPA has indices a, b, c, and d, in descending order of significance, and offset o in bits 11:0. The letters R, W, and X in the last column indicate permissions set at that entry.

Table base Selected index Entry points to Permissions
T4 from EPTP a T3, a PDPT R W X
T3 b T2, a PD R W X
T2 c T1, a PT R X
T1 d F, a 4 KiB leaf base R W X

Use T4 + 8*a to locate the first entry. Its output is the base T3, so use T3 + 8*b for the next lookup. The PD entry is at T2 + 8*c, and the PT entry is at T1 + 8*d. The final HPA is F + o. Under the basic EPT permission format shown here, a read can use that mapping, and a write is denied because the PD entry clears W. The PT entry's own write bit is set. This is why a host-side dump needs the entries above the leaf when the question is about access permission.

For a captured dump, first match the root to the VM and vCPU context you are inspecting, then record the input GPA and the entry selected at each level. Track whether each entry continues the walk or ends it. The address field of a leaf gives the mapped frame or block base. The physical address of the entry itself tells you where the mapping is stored. Use the first of those addresses in your HPA calculation. If a live snapshot spans changes to the active mappings, entries read at different times may fail to describe a single walk. A consistent snapshot lets you check one mapping state. The symbolic snapshot above can be inspected on Ubuntu even when the outer hypervisor does not expose hardware virtualization.

Recover an HPADecode EPTPIndex PML4GPA bits47:39Index PDPTGPA bits38:30Index PDGPA bits29:21Index PTGPA bits20:12Add byteoffsetGPA bits11:0Decode EPTPIndex PML4GPA bits47:39Index PDPTGPA bits38:30Index PDGPA bits29:21Index PTGPA bits20:12Add byteoffsetGPA bits11:0
Each index selects an entry, and the leaf base plus the GPA offset gives the HPA
Going deeperEPT exits and large mappings

An EPT violation occurs when the EPT access checks block an access, for example because an entry has all three basic permission bits clear or the path denies writes. With ordinary VM-exit delivery, its basic Intel VM-exit reason is 48 (0x30). The EPT-violation exit qualification describes the access type and includes information for distinguishing a fault during guest linear-address translation from one on the resulting GPA when the relevant qualification fields are valid. An EPT misconfiguration occurs when hardware encounters an invalid EPT entry encoding, for example a permission encoding with write set and read clear. Its basic VM-exit reason is 49 (0x31). Those numbers are VMX basic exit reasons.

For an ordinary RAM access that encounters a missing EPT mapping, the KVM MMU can resolve the backing, install an appropriate entry, and let the vCPU retry. KVM can also deliberately restrict access in a valid mapping, for instance by write-protecting a page for dirty tracking. A misconfiguration requires interpreting the offending encoding and its context. KVM can deliberately use a hardware-invalid EPT encoding for cached MMIO information, so the hardware exit reason alone does not diagnose a KVM defect.

In EPT, a supported 2 MiB leaf can end the walk at the PD, and a supported 1 GiB leaf can end it at the PDPT. Use the low 21 or 30 GPA bits, respectively, as the offset into the aligned host physical range. Both cases let hardware skip lower EPT levels. A large leaf requires a suitably aligned, contiguous host physical range that can use uniform attributes and permissions. Large mappings can also increase the amount of memory covered by cached translations. The coverage of a combined translation still depends on the limits of both stages, so a large EPT leaf alone does not imply a large combined TLB entry.

Going deeperMemory type across the two translations

In Intel EPT, the leaf's memory-type field describes the mapped memory, and the leaf's ignore-PAT bit controls whether the guest paging translation's PAT-selected type participates. With ignore-PAT clear, hardware combines the PAT-selected type and the EPT leaf type using Intel's combination rules. For example, a guest PAT selection of write-back cannot make an EPT mapping of uncacheable memory effectively write-back. With ignore-PAT set, the EPT leaf type governs the access without the guest PAT contribution.

MTRRs describe types for physical ranges. A guest's virtual MTRRs describe guest physical ranges and can inform hypervisor policy. The processor does not independently apply those virtual MTRRs to accesses through EPT. With EPT active, the hardware's type calculation for the translated guest access uses the EPT leaf and, where applicable, guest PAT. The hypervisor must choose mappings consistent with the real backing and avoid conflicting effective types for aliases of a physical page. EPTP's memory-type field concerns the accesses that read the EPT paging structures themselves. When investigating a type mismatch, identify the mapped address, inspect the EPT leaf's type and ignore-PAT bit, and decode the guest PAT selection if it contributes.

“Why nested paging wins and the TLB cost” follows the same memory access with translation caching in view. It asks which translations a TLB hit can reuse and what work remains when the needed translation is missing.

Key takeaways

  • Hardware applies the second translation stage to guest page-table accesses as well as to the final guest physical address.
  • EPTP and nCR3 identify x86 second-stage roots, while Arm uses VTTBR_EL2 and RISC-V uses hgatp.
  • An EPT leaf supplies the host physical base and its position in the hierarchy determines the offset width.
  • An EPT violation reports a blocked access, while an EPT misconfiguration reports an invalid entry encoding.
Reflect
  1. Explain to a colleague, in two sentences: trace a guest memory access through both translation stages.
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Official documentation first. Links checked when the lesson was published.

Find your next step

↑ ↓ to move · Enter to open · Esc to close