11.3 Stage 2 in detail
Stage 2 translates an intermediate physical address (IPA) to a PA for every EL1&0 access while HCR_EL2.VM=1. That includes the guest’s own stage-1 table walks, because the guest’s TTBRn_EL1 and its table descriptors hold IPAs. The descriptor formats and the walk mechanics are the same as stage 1 (5.3). This section covers what is different: the configuration register, the attribute and permission fields, how stage 1 and stage 2 results combine, and the controls that override that combination.
VTCR_EL2 and the IPA size
| Field | Bits | Meaning |
|---|---|---|
T0SZ | [5:0] | IPA size = 64 − T0SZ bits |
SL0 | [7:6] | Starting level of the walk (encoding depends on TG0) |
IRGN0, ORGN0, SH0 | [9:8], [11:10], [13:12] | Cacheability and shareability of stage-2 table walks |
TG0 | [15:14] | Granule: 0b00 4KB, 0b01 64KB, 0b10 16KB |
PS | [18:16] | Output (physical) address size: 0b000 32 bits … 0b101 48, 0b110 52 |
VS | [19] | 16-bit VMID |
HA, HD | [21], [22] | Hardware Access-flag and dirty-state update for stage 2 (FEAT_HAFDBS) |
NSW, NSA | [29], [30] | For Secure EL2: PA space of Secure stage-2 walks and output |
DS | [32] | 52-bit addressing with 4KB/16KB granules (FEAT_LPA2) |
SL2 | [33] | With DS=1 and 4KB granule, start at level −1 |
There is only one range (no T1SZ), and the IPA space starts at 0. Stage 2 has one feature stage 1 does not: concatenated tables at the starting level. Up to 16 adjacent tables can form the first level, which adds up to four bits of IPA without adding a walk level. KVM’s default 40-bit IPA with a 4KB granule uses T0SZ=24, SL0=0b01 (start at level 1) and two concatenated level-1 tables, which needs 8KB contiguous and 8KB-aligned, instead of a four-level walk. The T0SZ/SL0 combination must be consistent with the granule. An inconsistent pair is not a valid configuration. The VTCR_EL2.SL0 description says how it is reported (a stage-2 Translation fault in the pseudocode), so check it before relying on the exact behaviour.
Two size checks apply and are often confused:
- An IPA above 2^(64−T0SZ) gives a stage-2 Translation fault. This is how a hypervisor bounds guest physical address space.
- An output address above the
PSsize, or above the implementedID_AA64MMFR0_EL1.PARange, gives an Address size fault. ProgramPSno higher thanPARange(a larger value is treated as the implemented size, so it buys nothing). On heterogeneous systems, use the smallestPARangeof all cores (the sanitised value).
Bring-up note: KVM sizes each VM’s IPA space from the VMM’s request, bounded by the sanitised
PARange. On SoCs that put DRAM or device apertures above 2^40, a 40-bit default IPA leaves no room for a device passthrough at its native address. VMMs then relocate it, or ask for a larger IPA, which needs a deeper walk or more concatenation.
Stage-2 descriptor fields
The upper and lower attributes differ from stage 1 in three ways. Stage 2 has no AttrIndx or MAIR, no user/privileged split in its permissions, and no nG bit.
| Field | Bits | Meaning |
|---|---|---|
MemAttr[3:0] | [5:2] | Memory type, encoded directly (no MAIR indirection) |
S2AP[1:0] | [7:6] | 00 no access, 01 read-only, 10 write-only, 11 read/write |
SH[1:0] | [9:8] | Shareability |
AF | [10] | Access flag |
DBM | [51] | Dirty bit modifier (hardware dirty tracking) |
XN[1:0] | [54:53] | Execute-never. With FEAT_XNX, the two bits give separate EL1 and EL0 control |
With HCR_EL2.FWB=0, MemAttr is interpreted like this. MemAttr[3:2]=00 means Device, with MemAttr[1:0] giving nGnRnE/nGnRE/nGRE/GRE. Otherwise MemAttr[3:2] is the Outer and MemAttr[1:0] the Inner cacheability: 01 Non-cacheable, 10 Write-Through, 11 Write-Back. Stage 2 carries no allocation or transient hints. Those come from stage 1. FEAT_S2PIE and FEAT_S2POE (permission indirection and overlays for stage 2, v8.9) let S2PIR_EL2 reinterpret the permission bits. They are not covered further here.
How stage 1 and stage 2 combine
This is the single place in the book where combination is defined (6.5 points here). With FWB=0 the rule is the more restrictive of the two, for each attribute:
| Attribute | Combined result |
|---|---|
| Memory type | If either stage says Device, the result is Device, of the stronger type: nGnRnE > nGnRE > nGRE > GRE |
| Cacheability (Inner and Outer separately) | The weaker: Non-cacheable < Write-Through < Write-Back |
| Allocation and transient hints | From stage 1 |
| Shareability | If the result is Device or Inner+Outer Non-cacheable, Outer Shareable. Otherwise the more shareable of the two: OSH > ISH > NSH |
| Data permissions | Both must permit. Stage 1 faults are taken to EL1, stage 2 faults to EL2 |
| Execute | Execute-never if either stage says so |
Two consequences matter in practice.
A guest can always make memory less cacheable, never more. The hypervisor maps guest RAM as stage-2 Normal WB. The guest still gets Device-nGnRnE for data when its MMU is off (6.5), and Non-cacheable if it maps a buffer that way. Meanwhile the host may have written the guest’s kernel image through its own cacheable alias. The guest’s non-cacheable reads then go past dirty lines in the cache to stale DRAM, which is the classic “guest crashes at boot with garbage instructions” bug. Before FWB, KVM handled this by cleaning every page to the PoC before mapping it at stage 2, and by trapping set/way operations and (via TVM) MMU-enable writes so that it could flush when the guest switched its caches on (8.5, 6.6).
The hypervisor’s Device mapping always wins. Mapping emulated MMIO as stage-2 Device, or leaving it unmapped so that accesses fault and are emulated, guarantees Device semantics whatever the guest’s stage 1 says. HCR_EL2.PTW=1 additionally makes a guest stage-1 walk that lands on stage-2 Device memory a stage-2 Permission fault, rather than a walk of MMIO.
FEAT_S2FWB: stage 2 forced write-back
FEAT_S2FWB (v8.4) changes the meaning of MemAttr when HCR_EL2.FWB=1, so that stage 2 can force an attribute instead of only restricting it. MemAttr[2] selects the interpretation:
MemAttr[2:0] | Result |
|---|---|
0b0xx | Device, of the type in MemAttr[1:0]. FWB does not change how a stage-2 Device type combines with stage 1, so a stronger stage-1 Device type still wins |
0b101 | Normal Non-cacheable, unless stage 1 is Device, in which case the stage-1 Device type |
0b110 | Normal Write-Back, regardless of stage 1 (allocation hints from stage 1 if it is Normal cacheable) |
0b111 | The stage-1 attributes, unchanged |
MemAttr[3] is RES0 unless FEAT_MTE_PERM uses it (13.6). With guest RAM mapped 0b110, every guest access to RAM is Write-Back cacheable even while the guest’s MMU is off. There are no mismatched aliases with the host, and KVM skips the clean-to-PoC on stage-2 mapping. The cost is that a guest can no longer obtain a genuinely Non-cacheable view of RAM, for example for a non-coherent passthrough device’s buffers. Such regions must be mapped with 0b101 or 0b111 deliberately. Instruction-side maintenance (I-cache invalidation to the PoU, 8.6) is unaffected by FWB.
HCR_EL2.CD, ID and DC
These are older, global overrides.
HCR_EL2.CD(bit 32): stage 2 is treated as Normal Non-cacheable for all EL1&0 data accesses and stage-1 walks.HCR_EL2.ID(bit 33): the same for instruction fetches.
These make the whole guest uncacheable without touching stage-2 tables. They are useful as a debugging lever (“does the bug go away if the guest is uncached?”) and not otherwise used by KVM. How CD and ID interact with FWB=1 encodings is spelled out in the HCR_EL2.CD/ID field descriptions. Check them rather than assuming that one override beats the other.
HCR_EL2.DC(bit 12, “default cacheable”): the PE behaves as ifSCTLR_EL1.Mwere 0 whatever the guest writes (stage 1 forced off), the default stage-1 attributes become Normal Inner/Outer Write-Back Non-shareable instead of Device-nGnRnE, andHCR_EL2.VMbehaves as 1. It suits only a guest that never enables its own MMU, so it was never a general answer to the MMU-off coherency problem. FWB is.
Stage-2 faults
A stage-2 fault is taken to EL2 as an instruction or data abort from a lower EL (EC 0x20/0x24). Decoding is in 5.8. The points specific to stage 2:
ESR_EL2.ISS.S1PTW=1means the fault happened on the guest’s stage-1 walk: the IPA of a guest table descriptor was unmapped or not readable at stage 2. A hypervisor that reports this to the guest as a guest page fault is wrong. It is the hypervisor’s own mapping problem.FAR_EL2holds the guest VA, andHPFAR_EL2.FIPAholds the faulting IPA bits [51:12] (or [47:12]). HPFAR is valid for stage-2 Translation and Access flag faults and for faults on stage-1 walks. For a stage-2 Permission fault not on a walk, Linux treats HPFAR as possibly invalid and recomputes the IPA withAT S1E1Ron the guest VA.- MMIO emulation relies on
ISS.ISV=1withSAS,SSE,SRT,SFandWnR. Syndrome is provided only for single general-purpose-register loads and stores without writeback. A guest that touches MMIO withLDPor a post-indexed load givesISV=0, and the hypervisor must decode the instruction or kill the guest. Guest drivers should use simpleLDR/STRfor MMIO.
Validation note: the combination table is directly testable. From EL2, run
AT S12E1R(orS12E1W) on a guest VA with a chosen stage-1 MAIR attribute and stage-2MemAttr.PAR_EL1.ATTRandPAR_EL1.SHthen report the combined result in MAIR format (thePAR_EL1description allows the reported attributes to differ from an IMPLEMENTATION DEFINED downgrade the core actually applies, such as WT treated as NC, so treat AT as checking the architectural rule). Sweep the 4×4 matrix of {Device-nGnRnE, NC, WT, WB} for both FWB=0 and FWB=1 and compare with the tables above. A mismatch in a real access path, as opposed to AT, shows up as unexpected L1D refills (L1D_CACHE_REFILL) on a region that should be non-cacheable, or none on one that should be cacheable (16.5).
Checkpoint
Q11.3.1 With HCR_EL2.FWB=0, the guest’s stage 1 maps a buffer as Normal Inner/Outer Write-Back Inner Shareable, and the hypervisor’s stage 2 maps it as Normal Outer Write-Back, Inner Non-cacheable. What is the combined attribute?
- A. Normal Inner/Outer Write-Back, Inner Shareable, because stage 1 controls cacheability and stage 2 only controls permissions
- B. Normal Inner/Outer Non-cacheable, Outer Shareable, because any non-cacheable component makes the whole access non-cacheable
- C. Normal Inner Non-cacheable, Outer Write-Back, with shareability the more shareable of the two descriptors’
SHfields - D. Device-nGnRE, because mismatched cacheability is CONSTRAINED UNPREDICTABLE
Answer
C. Inner and outer cacheability are combined separately, each taking the weaker value. Shareability is the more shareable of the two, and forced Outer Shareable only when the result is Device or Non-cacheable at both levels. B wrongly collapses inner and outer. A ignores that stage 2 restricts attributes as well as permissions.
Read more: Arm ARM (DDI0487), “Combining the stage 1 and stage 2 attributes”
Q11.3.2 A hypervisor on a core without FEAT_S2FWB copies a guest kernel into guest RAM through its own Write-Back mapping, maps the RAM at stage 2 as Normal WB, and starts the guest with its MMU off. The guest executes garbage. Which fix is architecturally sufficient?
- A. Map the guest RAM at stage 2 as Device-nGnRnE so the guest’s accesses are ordered
- B. Clean the copied range to the PoC (and invalidate the I-cache to the PoU) before running the guest, because the guest’s MMU-off accesses are non-cacheable and bypass the dirty lines
- C. Set
HCR_EL2.CD=1so that the hypervisor’s own writes become non-cacheable - D. Nothing at the hypervisor; the guest must invalidate its caches before turning the MMU on
Answer
B. The combined attribute is the more restrictive one. With the guest’s stage 1 off, data is Device-nGnRnE, so reads go to memory past the host’s dirty lines. Cleaning to the PoC makes memory up to date. The I-cache invalidation is needed because, once the guest enables its I-cache, it could otherwise hit stale instruction lines held for those PAs from earlier use. C affects guest EL1&0 accesses, not the hypervisor’s EL2 writes. D fails because the guest reads its image with the MMU off before it can run any maintenance, so it has already fetched stale DRAM contents. With FWB, mapping 0b110 removes the problem.
Read more: Arm ARM (DDI0487), HCR_EL2.FWB; “Behavior of caches at reset and when caches are disabled”
Q11.3.3 A data abort reaches EL2 with ESR_EL2.ISS.S1PTW=1 and a stage-2 Translation fault DFSC. What does it mean, and what should the hypervisor do?
- A. The guest’s own page tables have an invalid entry; inject a data abort into the guest
- B. The guest accessed MMIO; emulate the access using the
SRTfield - C. The fault address in
FAR_EL2is an IPA; map it and retry - D. The guest’s stage-1 walk read a descriptor at an IPA that stage 2 does not map; fix or populate the stage-2 mapping of that IPA (from
HPFAR_EL2) and retry
Answer
D. S1PTW marks a stage-2 fault during the stage-1 table walk. The faulting IPA is the descriptor’s address, reported in HPFAR_EL2, and the cause lies in the hypervisor’s stage 2, not the guest’s tables (A). FAR_EL2 holds the guest VA of the original access, not an IPA (C). MMIO emulation (B) does not apply to walks.
Read more: Arm ARM (DDI0487), ESR_EL2 ISS encoding for Data Abort (S1PTW); HPFAR_EL2