Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

13.4 MTE architecture

The Memory Tagging Extension (MTE) attaches a small “colour” to every 16 bytes of memory and to every pointer, then has the hardware compare the two on each checked access. It is a lock-and-key scheme. The allocator chooses the colours, and the hardware catches use-after-free and buffer overflows when a pointer of one colour touches memory of another. This section covers the data model and the instructions. 13.5 covers when checks happen and how failures are reported, and 13.6 covers what the platform must provide.

Feature levels

ID_AA64PFR1_EL1.MTE [11:8] reports one of four levels:

ValueFeatureWhat you get
0b0000—Nothing
0b0001FEAT_MTE (v8.5)Instructions only at EL0: IRG, ADDG, SUBP and so on manipulate pointer tags, but no allocation tags are stored. Tag loads read as zero and tag stores are ignored
0b0010FEAT_MTE2Full MTE: allocation-tag storage, tag checking, the EL1 bulk instructions, the tag cache-maintenance operations
0b0011FEAT_MTE3MTE2 plus asymmetric checking (13.5)

All the features are optional, including in Armv9-A. ID_AA64PFR1_EL1.MTEX and ID_AA64PFR2_EL1 report the later extensions (MTE4, MTE_PERM, MTE_TAGGED_FAR, MTE_STORE_ONLY), which 13.6 covers. In practice “supports MTE” means MTE2 or higher. FEAT_MTE alone exists so that a binary using the instructions runs without faulting on a core that has no tag storage.

Allocation tags and logical tags

  • A logical tag (or address tag) is 4 bits held in bits [59:56] of a virtual address. It is carried in the TBI byte, so it costs no translation (13.3).
  • An allocation tag is 4 bits held in memory, one per 16-byte, 16-byte-aligned granule of physical memory. The tag belongs to the physical granule, not to the VA. Two VAs that alias the same PA share its allocation tags.

Allocation tags exist only for memory whose final memory attribute is Tagged Normal. At stage 1 that is the MAIR encoding 0xF0, Inner and Outer Write-Back, Read-Allocate, Write-Allocate, Non-transient, plus tagging (6.5). Stage 2 and system controls can withdraw it (13.6). For any other memory type, tag reads return zero, tag writes are ignored, and accesses are unchecked.

Tag storage costs 4 bits per 128 bits, or 1/32 (3.125%) of the tagged memory. Where the tags live is not architectural. The usual implementation is a carve-out of DRAM that the memory controller manages. In the cache hierarchy, tags may travel with their data lines or in separate tag caches, and that is IMPLEMENTATION DEFINED. Software sees only the instructions below.

Access control: who may touch allocation tags

The instructions that read or write allocation tags work only when allocation tag access is enabled for the current EL:

  • SCTLR_EL1.ATA [43] for EL1 and ATA0 [42] for EL0 (and the equivalent bits in SCTLR_EL2/EL3);
  • and, from above, HCR_EL2.ATA [56] and SCR_EL3.ATA [26] (13.6).

When access is disabled, LDG reads a tag of 0 and STG-class stores update only data (or nothing). Pointer-only instructions such as IRG and ADDG still work. This is separate from checking: ATA controls whether software may access tags, while TCF controls whether loads and stores are checked against them.

The instructions

Pointer manipulation (no memory access)

InstructionEffect
IRG Xd|SP, Xn|SP{, Xm}Insert Random taG: copies Xn to Xd with a random logical tag, excluding the tags in GCR_EL1.Exclude OR’d with Xm[15:0]
GMI Xd, Xn|SP, XmTag Mask Insert: Xd = Xm | (1 << tag(Xn)), which builds an exclusion set (“not the neighbour’s tag”)
ADDG Xd|SP, Xn|SP, #uimm6, #uimm4Adds uimm6 × 16 to the address and uimm4 to the tag, skipping excluded tags
SUBG ...The same with subtraction
SUBP Xd, Xn|SP, Xm|SP / SUBPS / CMPP56-bit pointer difference or compare that ignores the tags

ADDG exists because the compiler tags each stack slot differently. It steps both the address and the colour in one instruction, staying clear of colours the allocator excludes.

Tag stores

InstructionWrites
STG Xt|SP, [Xn|SP{, #simm}]The allocation tag of one granule := the logical tag of Xt. The address comes from Xn. Pre- and post-index forms exist
STZGSame, and zeroes the 16 data bytes
ST2G / STZ2GTwo consecutive granules (32 bytes)
STGP Xt1, Xt2, [Xn|SP{, #simm}]16 bytes of data and the tag taken from the address register
DC GVA, XtTags of a whole DCZID_EL0 block, set from the tag in Xt
DC GZVA, XtZeroes the block’s data and sets its tags: the tagging form of DC ZVA

The Xt versus Xn distinction in STG matters. stg x0, [x0] (the common form) colours the granule with the pointer’s own tag. stg x1, [x0] colours the granule that x0 points to with x1’s tag, which is how a free routine retags memory to a “poison” colour through the old pointer.

Tag loads

InstructionReads
LDG Xt, [Xn|SP{, #simm}]Puts the allocation tag of the addressed granule into Xt[59:56] and leaves the other bits of Xt unchanged. ldg x0, [x0] therefore “re-colours” an untagged pointer correctly

Bulk forms (EL1 and above only)

STGM Xt, [Xn], STZGM Xt, [Xn] and LDGM Xt, [Xn] move the tags of a whole block of granules to or from a register in packed 4-bit form. The block size is 4 << GMID_EL1.BS bytes, and GMID_EL1 exists for exactly this. These forms are UNDEFINED at EL0 and are the kernel’s tool for clearing, saving and restoring a page’s tags (swap, migration, hibernation). Linux’s mte_clear_page_tags is simply:

    mrs     x1, gmid_el1
    ubfx    x1, x1, #0, #4
    mov     x2, #4
    lsl     x1, x2, x1          // bytes per STGM
1:  stgm    xzr, [x0]           // tag := 0 for the whole block
    add     x0, x0, x1
    tst     x0, #(PAGE_SIZE - 1)
    b.ne    1b

An allocator tagging a fresh 64-byte object looks like this:

    irg     x0, x0              // random tag, honouring GCR_EL1.Exclude
    mov     x1, x0
    add     x2, x0, #64
1:  st2g    x1, [x1], #32       // colour two granules, post-increment
    cmp     x1, x2
    b.ne    1b                  // x0 = tagged pointer returned to the caller

All tag stores are not checked. Writing tags is a privileged operation for the allocator (controlled by ATA), not an access to the data itself.

Tag cache maintenance

Because tags can be cached, possibly in different structures from the data, MTE2 adds tag variants of the data cache maintenance operations (8.5). The naming pattern is G for “tags only” and GD for “tags and data”:

OperationEffectPoint
DC CGVAC / DC CGDVACClean tags / tags and dataPoC
DC CIGVAC / DC CIGDVACClean and invalidate tags / tags and dataPoC
DC IGVAC / DC IGDVACInvalidate tags / tags and data (EL1 and above)PoC
DC CGVAP / DC CGDVAPClean to the Point of PersistencePoP
DC CGVADP / DC CGDVADPClean to the Point of Deep PersistencePoDP
DC IGSW, IGDSW, CGSW, CGDSW, CIGSW, CIGDSWSet/way forms, local only—

A data-only operation (DC CIVAC) is not required to maintain the allocation tags. Code that hands Tagged memory to an observer that bypasses the PE’s caches must therefore use the GD form. Examples are a non-coherent agent that copies pages with their tags, a memory-controller tag scrubber, or a power-down path that turns caches off (8.8). The set/way variants share every set/way caveat from 8.5. They exist for power-down and boot, not for coherency.

Bring-up note: DC GZVA is gated by the same DCZID_EL0.DZP bit as DC ZVA (prohibited when DZP = 1). Linux falls back to STZ2G loops in that case. Firmware that zeroes Tagged memory with DC ZVA leaves its old tags in place. The first reuse of that memory with a new tag then faults if the old tags were non-zero.

Checkpoint

Q13.4.1 x0 holds a pointer with logical tag 0x5, and x1 holds the same address with logical tag 0xA. After stg x1, [x0], what is the allocation tag of the granule?

  • A. 0x5, because the address register’s tag is stored
  • B. 0xA, because STG stores the logical tag of the data register Xt at the address in Xn
  • C. 0x5 ⊕ 0xA
  • D. Unchanged, because STG is checked and the tags of x0 and the granule may not match
Answer

B. STG Xt, [Xn] takes the tag from Xt and the address from Xn. This is what lets a free routine re-colour memory through the old pointer. A describes STGP, which takes the tag from the address register. D is wrong because tag stores are not tag-checked.

Read more: Arm ARM (DDI0487), STG; Allocation Tag access

Q13.4.2 An EL1 driver hands a 4 KB Tagged page to a non-coherent DMA engine that copies pages including their allocation tags through the memory controller’s tag interface. Which maintenance before starting the DMA is sufficient?

  • A. DC CVAC over the page, then DSB SY
  • B. DC CGVAC over the page, then DSB SY
  • C. DC CIVAC over the page; tags are always written back with their data line
  • D. DC CGDVAC over the page, then DSB SY, so that both the dirty data and the dirty tags reach the PoC before the DMA reads them
Answer

D. The engine reads both data and tags from beyond the caches, so both must be clean to the PoC. CGDVAC covers both, and the DSB ensures completion. A and C are data-only operations, and the architecture does not require them to maintain tags; whether tags share a line with data is IMPLEMENTATION DEFINED. B cleans only the tags, so dirty data stays in the cache.

Read more: Arm ARM (DDI0487), DC CGDVAC; Memory Tagging Extension, cache maintenance

Q13.4.3 On a core reporting ID_AA64PFR1_EL1.MTE = 0b0001, a user program executes irg x0, x0; stg x0, [x0]; ldr x1, [x0] on a mapping with MAIR attribute 0xF0. What is observed?

  • A. IRG is UNDEFINED
  • B. The LDR takes a tag check fault on the first mismatch
  • C. All three execute. The STG stores no tag, and the LDR is not checked, because FEAT_MTE without MTE2 has no tag storage or checking
  • D. STG takes a permission fault because tag storage is not mapped
Answer

C. FEAT_MTE alone is the instructions-only level: the EL0 tag instructions execute, but allocation tags are not stored and accesses are not checked, so tagged binaries run unprotected but without faults. Checking (B) needs FEAT_MTE2 together with the enables described in 13.5 and 13.6.

Read more: Arm ARM (DDI0487), ID_AA64PFR1_EL1.MTE; FEAT_MTE and FEAT_MTE2