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

12.5 Memory Encryption Contexts (FEAT_MEC)

RME’s GPC (12.4) separates the worlds. It does not separate one Realm from another: every Realm granule is in the same Realm PAS, so isolation between Realms rests entirely on the RMM’s stage 2 tables. Against physical attacks (bus probing, cold boot, DRAM interposers), a system can encrypt each PAS with its own key, but without MEC all of Realm PAS shares one key. FEAT_MEC (permitted from v9.2, requires FEAT_RME; detected with ID_AA64MMFR3_EL1.MEC) splits the Realm PAS into many Memory Encryption Contexts. Each memory access carries a MECID that selects the encryption context (key or key tweak) used at the memory encryption engine. Accesses to the Non-secure, Secure and Root PASes always use the default MECID 0. Realm PAS accesses use whatever MECID the translation regime and descriptor select.

The architecture defines how a MECID is attached to an access and what software must do to avoid mixing contexts. It does not define the cipher, the key store or the engine. Those are system components (DEN0129 calls the engine a Memory Protection Engine), driven by IMPLEMENTATION DEFINED EL3 firmware.

The registers

RegisterSelects the MECID forNotes
MECIDR_EL2— (ID register)MECIDWidthm1 [3:0]: MECID width is MECIDWidthm1 + 1 bits, up to 16
MECID_P0_EL2Realm EL2 / EL2&0 accesses translated by TTBR0_EL2 (primary)
MECID_A0_EL2The same, via descriptors with AMEC=1 (alternate)Enabled by TCR2_EL2.AMEC0 [12]
MECID_P1_EL2Realm EL2&0 accesses translated by TTBR1_EL2 (primary, E2H=1)
MECID_A1_EL2The same, via descriptors with AMEC=1Enabled by TCR2_EL2.AMEC1 [13]
VMECID_P_EL2Realm EL1&0 stage 2 (primary): the Realm VM’s own memory
VMECID_A_EL2Realm EL1&0 stage 2 (alternate), via stage 2 descriptors with AMEC=1One alternate context per Realm VM
MECID_RL_A_EL3EL3 accesses to Realm PAS (descriptor {NSE,NS}={1,1})Used when SCTLR2_EL3.EMEC [1] = 1

The enables around them:

  • SCR_EL3.MECEn (bit 49) lets Realm EL2 access the MECID registers; otherwise they trap to EL3. TF-A gates this behind ENABLE_FEAT_MEC, which also needs FEAT_TCR2 and FEAT_SCTLR2.
  • SCTLR2_EL2.EMEC turns MEC on for Realm EL2’s regimes. While it is 0, the AMEC descriptor bit position is RES0 and no alternate MECIDs are used.
  • TCR2_EL2.AMEC0/AMEC1: when 0, using a block or page descriptor with AMEC=1 causes a Translation fault. Both bits may be cached in a TLB only when they are 1.
  • MEC applies only to Realm PAS accesses, so these registers and enables have no effect on Secure, Non-secure or Root PAS accesses (the access rules from other Security states are in each register’s description). They reset to UNKNOWN values on a Warm reset, so the RMM’s warm-boot path must reprogram them.

Device traffic is tagged as well: with MEC, SMMU-originated accesses to Realm memory also carry a MECID from the stream’s configuration (IHI0070).

How the RMM uses them

Arm’s MEC use-model guidance (document 109839) and TF-RMM follow the same pattern:

  • Per-Realm private MECID. When a Realm is created, the RMM gives it a MECID, either chosen by the host in the Realm parameters (RMM v1.1) or the default. The RMM sets VMECID_P_EL2 to that value when it runs the Realm’s RECs, so the Realm’s code, data, stage 1 tables and stage 2 tables are all encrypted in its own context.
  • RMM accessing Realm state. The RMM runs with E2H=1, so it has two VA halves, each with its own primary and alternate MECID register. Its own image stays in the RMM’s own context, and the mappings it uses for the Realm it is serving (in TF-RMM, the per-CPU slot buffer in the TTBR1_EL2 half) carry that Realm’s MECID, so it reads and writes the Realm’s RTTs, metadata and REC contexts in the Realm’s context. Which half and which register (primary or alternate) carry which context is a software choice; check the TF-RMM source for the current assignment.
  • Alternate contexts. VMECID_A_EL2/MECID_A0_EL2 give each Realm one extra context, intended for memory shared between Realms or with the RMM. The RMM specification exposes this as a shared MECID state (RMI_MEC_SET_SHARED / RMI_MEC_SET_PRIVATE in RMM v1.1). TF-RMM tracks one shared MECID and how many Realms use it, and refuses to make it private again while any Realm still does. Arm’s guidance notes that this does not make inter-Realm data sharing part of the CCA security model.
  • EL3. MECID_RL_A_EL3 usually holds the RMM’s MECID, so that EL3 can load the RMM image into Realm PAS at boot and write attestation material for it.
  • Key refresh. Reusing a MECID for a new Realm requires a new key or tweak for that MECID, otherwise the new Realm could read the old one’s ciphertext as plaintext. In TF-A’s RMM-EL3 interface this is RMM_MEC_REFRESH (0xC40001B6, from interface v0.8). It takes the MECID and a reason bit (Realm creation or destruction). How EL3 reprograms the engine is IMPLEMENTATION DEFINED. Refreshing a tweak destroys every byte still associated with the old value.

The Point of Encryption and the maintenance rules

The Point of Encryption (PoE) is the point in the memory system where accesses are encrypted and decrypted using their MECID. Caches before it hold plaintext. A cache line holds data for a PA. If it is filled under one MECID and later used or written back under another, the result is a mismatched-MECID access, and Arm states that such accesses corrupt memory within the Realm PAS. This is not a fault the PE detects. It is silent corruption, or plaintext from one context reaching another, which is a confidentiality break. Software must make it impossible.

Arm’s rules (109839, “Mismatched MECID avoidance”):

  1. All active translations to a granule use the same MECID. Two live mappings of one PA with different MECIDs (for example a primary and an alternate mapping) can produce a mismatch through ordinary or speculative accesses. A mapping counts as active until its TLB invalidation has completed.
  2. Do not change a MECID register while any granule is reachable through a translation that uses it.
  3. Clean and invalidate to the PoE before any change of a granule’s associated MECID or MECID state.

The instructions are FEAT_MEC’s DC CIPAE (clean and invalidate by PA to PoE, with the PAS selected in bits [63:62] as for DC CIPAPA) and DC CIGDPAE (the same, including MTE allocation tags). They can be executed at EL3, and at EL2 in Realm state. Realm EL2 software can therefore do its own MEC maintenance without an SMC.

The resulting flows for the RMM:

RemoveGranule(pa):                          // a granule leaves a Realm but stays in Realm PAS
    unmap from the Realm's stage 2   ; TLBI IPAS2E1IS ; DSB ISH ; TLBI VMALLE1IS ; DSB ISH   // see 9.1
    unmap any R-EL2 stage 1 alias    ; TLBI VAE2IS ; DSB
    for each line: DC CIPAE, pa | REALM
    DSB OSH

AddGranule(pa, ipa):                         // a granule joins a Realm
    if the previous MECID is still live:
        map temporarily at R-EL2 with the new Realm's MECID; scrub (DC ZVA or random data); unmap
    map pa at ipa in the Realm's stage 2

Points to note:

  • The GTF already covers PAS changes. A granule that leaves Realm PAS through the EL3 granule transition flow needs no extra DC CIPAE, because the GTF’s clean and invalidate to the PoPA already pushes it past the PoE. The PoE matters for granules that move within Realm PAS between MECIDs. TF-RMM therefore flushes to the PoE when a granule returns to the RMM’s DELEGATED state, since a delegated granule can be reused by another Realm without an undelegate.
  • Changing AMEC in a descriptor is destructive to the granule’s contents. Treat it as a break-before-make change (9.2) with a RemoveGranule-style flush in the middle.
  • Primary MECID registers can safely be changed by a higher EL while the lower regime is not running. That is the normal case for VMECID_P_EL2 on a REC switch, because the Realm’s mappings are not active while the RMM runs. Realm EL2 may turn on SCTLR2_EL2.EMEC itself only if MECID_P0_EL2 and MECID_P1_EL2 are 0, matching the MECID it was already using.
  • Alternate MECID registers can be changed only when no valid translation uses them and TLB entries with AMEC=1 have been invalidated. Clearing TCR2_EL2.AMEC0/1 first guarantees that no new AMEC=1 translation can be created while the register changes.

Security note: MEC defends against physical attackers and makes reused memory safe. It does not replace stage 2 isolation. MEC does not reliably catch a bug in the RMM’s RTT handling that maps Realm A’s page into Realm B. If the data is only in memory, B’s MECID decrypts it to garbage. If lines filled under A’s MECID are still cached, B may read A’s plaintext. If the bug also reuses A’s MECID, B reads the plaintext directly. Review MECID assignment code as carefully as RTT code.

Validation note: QEMU’s FEAT_MEC support models the registers (and, in recent versions, MECID selection) but does no encryption. On FVP, MEC is enabled with a model parameter, and TF-RMM reports whether it found FEAT_MEC. Neither environment will show the silent corruption a missing DC CIPAE causes on silicon. To test the maintenance rules pre-silicon, use an RTL or emulation build with the real memory protection engine. Write a pattern under MECID a, remap the granule under MECID b with the PoE flush deliberately omitted, and check that the corruption appears. Then confirm it disappears when the flush is restored.

Checkpoint

Q12.5.1 The RMM destroys Realm A and immediately gives one of its granules to a new Realm B, which has a different private MECID. The granule stays in Realm PAS the whole time. Which step is required for correctness, beyond unmapping and TLB invalidation?

  • A. An undelegate and re-delegate round trip through EL3, because Realm EL2 has no cache maintenance instruction that reaches past the PoC
  • B. DC CIVAC to the PoC through A’s old mapping before it is removed
  • C. Nothing, because B’s MECID selects a different key and A’s data becomes unreadable automatically
  • D. DC CIPAE on every line of the granule (to the PoE), then DSB, before B can access it, so that no line filled under A’s MECID survives in a cache to be hit or written back under B’s
Answer

D. Caches before the PoE hold plaintext, and the architecture gives no guarantee that a hit respects the MECID, so a line left over from A is a mismatched-MECID hazard. C is the tempting misconception: the key change protects only data that has already reached memory. A rests on a false premise: DC CIPAE is executable at Realm EL2 precisely so that the RMM can do this flush without an SMC, and the granule never needs to leave Realm PAS. B is the wrong point and relies on a mapping that should already be gone.

Read more: Arm ARM (DDI0487), FEAT_MEC and DC CIPAE; Arm RME and MEC example software flows (109839), Mismatched MECID avoidance

Q12.5.2 Realm EL2 software wants to change MECID_A0_EL2 while the system is running. Which approach is safe?

  • A. Write the register and issue an ISB, because MECID registers are not cached in TLBs
  • B. Clear TCR2_EL2.AMEC0 so no new AMEC=1 translations can be used, remove or invalidate every AMEC=1 mapping and its TLB entries, clean and invalidate the affected granules to the PoE, then write the register and re-enable
  • C. Clear SCTLR2_EL2.EMEC, write the register, then set EMEC again
  • D. Ask EL3 to issue RMM_MEC_REFRESH for the new value, which makes the change atomic
Answer

B. Alternate MECID registers may change only when no valid translation is associated with them and the AMEC=1 TLB entries are gone. AMEC0 exists to stop new ones being created during the change. Toggling EMEC (C) changes the MECID of every active Realm EL2 access, and that is itself a mismatch hazard. RMM_MEC_REFRESH (D) changes a MECID’s key, not which MECID a register selects.

Read more: Arm ARM (DDI0487), MECID_A0_EL2 and TCR2_EL2.AMEC0; Arm RME and MEC example software flows (109839), Update Alternate MECID register

Q12.5.3 Which statement about EL3’s use of MEC is correct?

  • A. EL3 always uses MECID 0, so it cannot access Realm memory once MEC is enabled
  • B. EL3 inherits MECID_P0_EL2, so it sees whatever Realm the RMM last ran
  • C. EL3 accesses to Secure, Non-secure and Root PAS use MECID 0. Accesses to Realm PAS use MECID_RL_A_EL3 when SCTLR2_EL3.EMEC=1, which is typically set to the RMM’s MECID so EL3 can load and serve the RMM
  • D. EL3 selects a MECID per access through the AMEC bit of its own descriptors
Answer

C. MEC contexts exist only in the Realm PAS, and EL3 has a single Realm-PAS MECID register of its own. A contradicts the RMM boot flow, in which EL3 populates Realm memory. B would couple EL3 to whatever state EL2 left behind. D describes Realm EL2’s alternate mechanism, not EL3’s.

Read more: Arm ARM (DDI0487), MECID_RL_A_EL3 and SCTLR2_EL3.EMEC; Arm RME and MEC example software flows (109839), EL3 translation regime