5.6 Access flag and dirty state
An OS that pages, reclaims or migrates memory needs two facts per page: has it been accessed since it was last aged, and has it been written since it was last cleaned? VMSAv8-64 records the first in the Access flag (AF) and the second in the dirty state. Both can be maintained by software through faults, or by hardware (FEAT_HAFDBS) that writes the descriptor itself. Once hardware writes descriptors, the walker is a writer of shared data, and software updates to page tables become a concurrency problem.
The Access flag
AF is bit [10] of every block and page descriptor, at both stages. Its meaning depends on whether hardware management is enabled for the regime:
| Configuration | Access to a descriptor with AF == 0 |
|---|---|
TCR_ELx.HA == 0 (or FEAT_HAFDBS not implemented) | Access flag fault, DFSC 0b0010LL (LL = level) |
TCR_ELx.HA == 1 | Hardware sets AF = 1 in memory with an atomic update, and the access proceeds |
HA is TCR_EL1[39] (and the VHE form of TCR_EL2), TCR_EL2[21]/TCR_EL3[21] in the single-range layouts, and VTCR_EL2[21] for stage 2. See the layout table in 5.2.
The software-managed scheme is simple. The OS clears AF when it ages a page, takes the Access flag fault on the next access, sets AF = 1, and returns. No TLB invalidation is needed on that path, because the architecture does not allow a TLB to cache a translation that generates an Access flag fault (the same rule applies to Translation and Address size faults). The new descriptor only has to be visible to the walker before the faulting instruction runs again: a store, a DSB, then the exception return. 9.1 gives the exact rules, including FEAT_ETS2.
Aging works the other way round. Clearing AF on a valid, possibly-cached entry has no effect until the TLB entry is gone, because a cached entry with AF == 1 keeps translating without a walk. If you need the next access to be detected, you need a TLBI. Linux accepts imprecise aging and skips the invalidation in some paths. Firmware that uses AF to detect accesses (rare) cannot.
Bring-up note: TF-A’s
xlat_tables_v2setsAF = 1in every descriptor it creates, and EL3 does not enableHA. An Access flag fault in early firmware almost always means a hand-built descriptor that leaves bit 10 clear. It shows as DFSC0x0B(level 3) or0x09/0x0Afor blocks. This is one of the five common mistakes in 5.9.
Dirty state and DBM
Armv8.0 has no dirty bit. Software uses write permission as a proxy: it maps a page read-only, takes a Permission fault on the first write, records the page as dirty, and makes the page writable. FEAT_HAFDBS adds a Dirty Bit Modifier (DBM, bit [51]) that lets hardware make that transition instead:
| Stage | “Writable-clean” encoding | What hardware does on a write (HD=1) | “Writable-dirty” encoding |
|---|---|---|---|
| Stage 1 | DBM = 1, AP[2] = 1 (read-only) | clears AP[2] to 0 | DBM = 1, AP[2] = 0 |
| Stage 2 | DBM = 1, S2AP[1] = 0 (no write) | sets S2AP[1] to 1 | DBM = 1, S2AP[1] = 1 |
So the “dirty bit” is not a separate bit. It is the write-permission bit, and DBM says “hardware may grant write permission on demand”. A descriptor with DBM = 0 and AP[2] = 1 is still truly read-only and gives a Permission fault. The enable is TCR_ELx.HD (TCR_EL1[40], VTCR_EL2[22]). HD has effect only when HA is also 1. With FEAT_S1PIE, the stage 1 dirty state moves to an explicit descriptor bit, and a permission fault caused by the dirty state is flagged by ESR_ELx.ISS2.DirtyBit. The PIE encodings are in 5.5.
To clean a page (after writeback, or at the start of a migration pass), software sets AP[2] = 1 again (or clears S2AP[1]). That is a permission downgrade. Cached writable entries keep allowing silent writes until they are invalidated, so the clean is a descriptor update followed by TLBI and DSB, not a store alone. Upgrading a page in the software scheme (read-only to writable after a fault) also needs a TLBI, unlike the AF case, because a TLB is permitted to cache an entry that generates a Permission fault. Linux’s ptep_set_access_flags() invalidates the page when it sets the dirty state for this reason.
Discovery and enables
ID_AA64MMFR1_EL1.HAFDBS (bits [3:0]) is cumulative:
| Value | Meaning |
|---|---|
0b0000 | No hardware updates |
0b0001 | Hardware Access flag only |
0b0010 | Access flag and dirty state (DBM) |
0b0011 | As 0b0010, plus FEAT_HAFT (Access flag in table descriptors) |
0b0100 | As 0b0011, plus FEAT_HDBSS |
FEAT_HAFDBS was introduced by the Armv8.1 extension and is OPTIONAL from Armv8.0. It has never become mandatory, so an Armv9 core may still report 0b0000 or 0b0001. HA/HD are per-regime, and on a heterogeneous system they can differ per core. Linux treats hardware DBM as a per-CPU capability for this reason, and disables it on cores with errata such as Cortex-A55 erratum 1024718.
Hardware updates and the memory model
A hardware AF or dirty-state update is an atomic read-modify-write of the descriptor, performed by the walker as part of translating an access. Several consequences follow.
1. The update is atomic with respect to software’s accesses, but software’s plain stores are not atomic with respect to it. If software reads a descriptor, computes a new value and stores it with an ordinary STR, hardware can set the dirty state between the read and the store, and the store silently discards it. For migration that is data corruption: the page is copied as clean while it holds a new write. Any software update of a descriptor that hardware may be updating concurrently must itself be an atomic RMW (LDXR/STXR or an LSE CAS, see 10.1 and 10.2), and must retry if the descriptor changed:
/* Write-protect (clean) a stage 1 PTE without losing a concurrent hardware dirty update. */
static inline uint64_t pte_wrprotect_atomic(volatile uint64_t *ptep)
{
uint64_t old = *ptep, new;
do {
new = old | PTE_RDONLY; /* AP[2] = 1: writable-clean if DBM = 1 */
/* if hardware cleared AP[2] since we read 'old', the CAS fails and we retry,
and the caller sees that the page was dirty */
} while (!__atomic_compare_exchange_n(ptep, &old, new, false,
__ATOMIC_RELAXED, __ATOMIC_RELAXED));
return old; /* old & PTE_RDONLY == 0 => was dirty */
}
/* then: DSB ISHST; TLBI VAE1IS; DSB ISH (9.1) */
Linux’s __ptep_set_wrprotect() and ptep_test_and_clear_young() are this loop with cmpxchg_relaxed().
2. The hardware side also detects races. If the descriptor changes between the walk’s read and the update, the hardware does not write a value computed from the stale read. DDI0487 requires the update to be retried against the new value, or the translation restarted. This is why the CAS loop above is sufficient: whichever writer goes second sees the other’s change.
3. The memory holding the tables must support atomic updates from the walker. If the walker’s RMW cannot be performed atomically on that memory, for example because the tables are Non-cacheable and the interconnect does not support far atomics to that target, the access takes an Unsupported atomic hardware update fault (DFSC 0x31). Whether a given memory type and location supports it is IMPLEMENTATION DEFINED. Keep tables in Normal Inner Write-Back Inner Shareable memory, and match the walk attributes in TCR_ELx.IRGN/ORGN/SH (5.2).
4. The AF is a hint. The dirty state is what migration depends on. The AF can be set by walks for accesses that are never architecturally executed, including speculative ones, so “AF set” means “probably accessed”, never “certainly accessed”. The dirty state is tied to a write being performed. Software relies on the rule that a write cannot become visible while its descriptor still reads as writable-clean. The exact statement of when the update becomes observable relative to the access, and the effect of DSB on outstanding hardware updates, is in DDI0487 “Hardware management of the dirty state” and the translation-table ordering rules. Read it before you write a lock-free page-table walker.
5. Ordering of the walker against your own loads still follows Part 9. A hardware update is not a program-order access of the PE that triggered it, so a plain load of the descriptor right after an access is not guaranteed to see the update. Software that reads AF or dirty state for decisions does so with atomics, and after the TLBI/DSB sequence when exactness matters.
Validation note: Test HAFDBS with a two-agent race: core A writes in a tight loop to a writable-clean page, while core B repeatedly cleans the page with a CAS loop plus
TLBI VAE1ISandDSB ISH, and records whetheroldwas dirty. Every write that A completes after B’sDSBmust be followed by B seeing the page dirty in a later iteration. A lost update shows up as a pass with a written value but “clean” status, which shows that the dirty update was not observed before the write was. Count walks with the PMU (DTLB_WALK, event0x0034) to confirm that the cleaning TLBIs are really forcing new walks. Also run with tables in Non-cacheable memory to check that the platform either supports the RMW or reports DFSC0x31cleanly instead of corrupting the descriptor.
FEAT_HAFT: Access flag in table descriptors
FEAT_HAFT (introduced by the Armv8.9 extension, OPTIONAL from Armv8.7, ID_AA64MMFR1_EL1.HAFDBS >= 0b0011) gives table descriptors an Access flag at bit [10], which was previously ignored there. With TCR2_ELx.HAFT (bit 11) or VTCR_EL2.HAFT (bit 44) set, the walker sets the table descriptor’s AF when it uses that table during a walk. An OS can then age memory hierarchically. If a level-1 table descriptor’s AF is still clear, nothing in the 1GB it covers has been walked since it was cleared, and the whole subtree can be skipped without visiting 262,144 PTEs. As with the leaf AF, clearing a table AF only has an effect once cached walk entries (including intermediate “walk cache” entries) have been invalidated. FEAT_HAFT requires FEAT_HAFDBS and FEAT_TCR2.
FEAT_HDBSS and FEAT_HACDBS: dirty tracking for live migration (overview)
Live migration of a VM is a stage 2 dirty-tracking loop: write-protect all of guest memory, let it run, find the dirty pages, copy them, and repeat. With plain HAFDBS the hypervisor has to scan stage 2 descriptors to find the dirty ones and clean each one with a CAS and a TLBI. Both are O(guest size) per pass. Two Armv9.5 extensions (OPTIONAL from Armv9.4) offload the work:
- FEAT_HDBSS (Hardware Dirty state tracking Structure). When
VTCR_EL2.HDBSS(bit 45) is set, each time hardware moves a stage 2 descriptor from writable-clean to writable-dirty it also appends a record of the IPA to a buffer in memory.HDBSSBR_EL2holds the buffer base and size (SZ, 4KB to 2MB).HDBSSPROD_EL2holds the producerINDEXand anFSCfor faults on the buffer write itself. A full buffer or a failed record write is reported to EL2 (ESR_ELx.ISS2.HDBSSF), so the hypervisor reads a list of dirty IPAs instead of scanning. - FEAT_HACDBS (Hardware Accelerator for Cleaning Dirty State). The hypervisor supplies a list of IPAs in memory (
HACDBSBR_EL2, with progress tracked inHACDBSCONS_EL2), and hardware moves each named stage 2 descriptor from writable-dirty back to writable-clean. Discovery isID_AA64MMFR4_EL1.HACDBS, and FEAT_HACDBS requires FEAT_HDBSS.
Both apply to stage 2 only and matter to hypervisor authors. For firmware, the job is to leave the EL2 controls alone and pass the ID fields through. Stage 2 itself is in 11.3.
Checkpoint
Q5.6.1 With TCR_EL1.HA == 1 and HD == 1, a kernel cleans a page by reading its PTE, ORing in AP[2], and writing it back with STR, then issues DSB ISHST; TLBI VAE1IS; DSB ISH. Under heavy writes from another core, the migration occasionally copies stale data. Why?
- A. The
TLBIshould beTLBI VMALLE1IS, because a VA-based invalidate misses walk-cache entries - B.
DSB ISHSTis too weak before theTLBI, so it must beDSB ISH - C. Hardware can clear
AP[2](mark the page dirty) between the kernel’s load and itsSTR, and the plain store overwrites that update, so the page is recorded as clean although it was written - D. Hardware dirty updates are not coherent with data caches, so the kernel must clean the PTE’s cache line first
Answer
C. The hardware update is an atomic RMW, but the kernel’s load/modify/store is not, so a concurrent hardware update is lost. The update must be a CAS (or LDXR/STXR) loop that fails if the descriptor changed. The barrier sequence (B) is the standard one from 9.1 and is not the problem. A TLBI VAE1IS (A) also invalidates the walk-cache entries for that VA. D is wrong: walker updates are coherent in the shareability domain given by the walk attributes.
Read more: Arm ARM (DDI0487), “Hardware management of the dirty state”; Linux __ptep_set_wrprotect()
Q5.6.2 In a purely software-managed scheme (HA == 0), which pair of fault fixes is correct?
- A. After an Access flag fault: set
AF,DSB, return, with noTLBI. After a write Permission fault used for dirty tracking: make the page writable and invalidate the page’s TLB entry - B. Both fixes need a
TLBI, because any descriptor change requires invalidation - C. Neither needs a
TLBI, because the faulting entry is never cached - D. The Access flag fix needs a
TLBI; the permission upgrade does not, because permission faults are re-checked on every access
Answer
A. A TLB is not permitted to cache a translation that generates an Access flag fault, so a walk after the fix sees the new descriptor. A TLB is permitted to cache an entry that generates a Permission fault, so an upgrade without invalidation can fault again. Option C is the tempting overgeneralisation of the AF rule.
Read more: Arm ARM (DDI0487), “TLB maintenance requirements” / caching of translation table entries that generate faults
Q5.6.3 A bring-up team enables TCR_EL1.HA on a new SoC whose early page tables sit in an SRAM mapped Non-cacheable. The first access to a page with AF == 0 reports DFSC 0x31. What does this tell you?
- A. The descriptor is malformed, so the walk returned a Translation fault encoded at level 1
- B.
HAis ignored unlessHDis also set - C. The SRAM has ECC, and the walk’s read took a synchronous parity error
- D. The walker’s atomic update of the descriptor is not supported for that memory, so the tables must move to memory that supports it (normally Normal Write-Back cacheable) or
HAmust stay off
Answer
D. 0x31 is the Unsupported atomic hardware update fault. Whether a memory type and target support the walker’s RMW is IMPLEMENTATION DEFINED, and Non-cacheable SRAM behind a simple slave often does not. B reverses the dependency: HD requires HA, not the other way round. C would be 0x1C–0x1F (or SEA codes with RAS).
Read more: Arm ARM (DDI0487), ESR_ELx.DFSC and “Hardware management of the Access flag”