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.1 TrustZone-A recap, precisely

TrustZone-A is two separate mechanisms that usually get described as one. The first is the Security state of the PE, which decides which system registers, translation regimes and exception routing are in force. The second is the physical address space (PAS) that each access targets, which is an extra address bit carried through caches, TLBs and the interconnect. The PE architecture defines both. Which physical memory a given PAS can actually reach is a system property, outside the Arm ARM.

Security state: SCR_EL3.NS and SCR_EL3.NSE

EL3 is always Secure (Root, when RME is implemented). The Security state of EL2, EL1 and EL0 comes from SCR_EL3:

SCR_EL3.NSE (bit 62)SCR_EL3.NS (bit 0)Lower-EL Security state
00Secure
01Non-secure
10Reserved
11Realm (FEAT_RME only)

Without FEAT_RME, NSE is RES0 and only NS matters. The value that counts is the one in force at the ERET (or other exception return) that leaves EL3. Changing SCR_EL3.NS while you stay at EL3 does not change EL3’s own state. It does change which copy of banked state EL3’s own accesses reach: for example, which ICC_* Group 1 registers MSR reaches, and the Security state used by AT S12E1R-style operations issued from EL3. Treat SCR_EL3 writes as context-changing: an ISB is needed before EL3 code that relies on the new value (7.8).

The other world-relevant SCR_EL3 controls you already know:

FieldBitEffect
IRQ, FIQ, EA1, 2, 3Route physical IRQ/FIQ/SError and External aborts to EL3
SMD7SMC is UNDEFINED at EL1, EL2 and EL3 (an HCR_EL2.TSC trap from EL1 takes priority)
HCE8HVC enabled
SIF9Secure state cannot fetch instructions from the Non-secure PAS
RW10Next-lower EL is AArch64
ST11Secure EL1 may access CNTPS_*_EL1 (otherwise trapped to EL3)
EEL218Secure EL2 enabled (FEAT_SEL2)
GPF48GPFs from EL0–EL2 taken to EL3 (FEAT_RME, see 12.4)

Most system registers below EL3 are not banked by Security state in AArch64. There is one SCTLR_EL1, one TTBR0_EL1, one VBAR_EL2. EL3 firmware must save and restore them on every world switch. This is the job of TF-A’s cm_el1_sysregs_context_save/restore(), and of the EL2 equivalents when CTX_INCLUDE_EL2_REGS is set (as it is when an SPMC runs at S-EL2 or the RMM at R-EL2). The real banking is in the GIC CPU interface (some ICC_* state, such as ICC_IGRPEN1_EL1, ICC_BPR1_EL1 and ICC_CTLR_EL1, has Secure and Non-secure copies). The Secure timers (CNTPS_*, and with FEAT_SEL2 CNTHPS_*/CNTHVS_*) are separate registers, not banked copies. This differs from AArch32, where many CP15 registers are banked, and it is why “forgot to switch register X” is a classic EL3 review finding.

The PAS: NS as an address bit

Every physical access carries a PAS identifier. In a two-world system that is the NS bit. Secure state may target either PAS: stage 1 descriptors in a Secure translation regime have an NS bit (bit 5 in block and page descriptors), and NSTable in table descriptors forces NS on everything below. Non-secure state can only target the Non-secure PAS, and the descriptor NS bit is ignored.

The rule that matters for the memory system: Secure PA 0x8000_0000 and Non-secure PA 0x8000_0000 are different locations as far as caches and TLBs are concerned. Cache lines and TLB entries are tagged with the PAS, so there is no architectural coherency between the two aliases, even if the system maps both to the same DRAM byte. Mapping a buffer Secure in one world and Non-secure in another is therefore an aliasing bug of the kind 6.6 describes. Shared buffers must be Non-secure in both worlds: OP-TEE maps its NS shared memory with NS=1.

TLB entries are also tagged with the translation regime and Security state, so a Secure EL1&0 entry is never used for a Non-secure EL1&0 lookup. TLBI instructions affect only the Security state of the regime they target: a TLBI VMALLE1IS issued at Secure EL1 does not touch Non-secure entries.

Security note: SCR_EL3.SIF is cheap insurance. If an S-EL1 mapping is accidentally NS and executable, the Normal world can plant code that Secure state will run. With SIF=1 such a fetch takes a Permission fault instead. It complements, and does not replace, SCTLR_EL1.WXN and a correct NS bit.

Secure memory: the TZASC is not architectural

The Arm ARM defines the two PASes. It says nothing about which DRAM is reachable through which PAS. That is the job of the system:

  • A TrustZone Address Space Controller (TZASC; Arm’s TZC-400 is the common example) sits in front of each DRAM port and checks every transaction’s NS attribute (AxPROT[1] on AXI, where 1 means Non-secure, or the CHI NS field) against per-region rules. It can also check a per-master ID (NSAID on TZC-400). A failed check typically returns a bus error (SLVERR/DECERR), which reaches the PE as an External abort or SError (3.4), or the read returns zeros, depending on how the TZC is configured.
  • A TrustZone Protection Controller (TZPC) or equivalent gates peripherals.
  • Non-PE masters drive NS themselves, or have it set by the SMMU’s stream configuration (11.5).

So “this region is Secure” means three settings agree: the TZASC region is configured, the Secure-world mappings use NS=0, and no Non-secure mapping reaches it with a PAS the TZASC lets through. Firmware (TF-A BL2 or BL31 platform code) programs the TZASC early and locks it if it can. RME replaces this with the PE-side Granule Protection Check, which works at granule size and can change at run time (12.4).

Bring-up note: a TZASC configured too late or too tightly looks like a memory-system bug. BL2 loads BL32 into DRAM and then BL31’s first access takes an SError. Before you suspect DDR training, check the TZC’s fault status registers (on TZC-400, INT_STATUS and the per-filter FAIL_ADDRESS_LOW/FAIL_ADDRESS_HIGH/FAIL_CONTROL/FAIL_ID) and its ACTION setting.

Secure EL2 (FEAT_SEL2)

FEAT_SEL2 (permitted from v8.4) adds EL2 to Secure state, enabled by SCR_EL3.EEL2. When EEL2=0, Secure state has only EL1 and EL0 (plus EL3), and Secure EL1&0 has no stage 2.

With EEL2=1, the Secure EL1&0 regime gets a stage 2 translation, and that stage 2 has two IPA spaces:

  • The Secure stage 1 descriptor NS bit now selects the IPA space (Secure IPA or Non-secure IPA), not the PA space directly.
  • Secure IPA space is translated through VSTTBR_EL2/VSTCR_EL2. Non-secure IPA space is translated through VTTBR_EL2/VTCR_EL2, the same registers the Non-secure hypervisor uses, so EL3 must context-switch them.
  • The output PAS is controlled by VSTCR_EL2.SW/SA for Secure IPA space and VTCR_EL2.NSW/NSA for Non-secure IPA space: *W covers the stage 2 walks, *A the final accesses.

All the usual EL2 machinery (HCR_EL2, VBAR_EL2, the GIC virtual interface) also exists in Secure state. Because the registers are shared, EL3 context-switches the whole EL2 register set when it switches between Secure EL2 and Non-secure EL2.

The secure partition model

Secure EL2 exists to stop the Secure world being one large trusted blob. Instead of a single Trusted OS at S-EL1 with authority over all Secure memory, an SPMC (Secure Partition Manager Core) runs at S-EL2 and isolates secure partitions (SPs) at S-EL1 and S-EL0, each confined by its own stage 2. The Arm reference is Hafnium. OP-TEE becomes one SP among several. The EL3 component that forwards calls between worlds is the SPMD, a dispatcher in BL31. The messaging, memory-sharing and partition-ID rules come from FF-A and are covered in 12.3. Without FEAT_SEL2 there are two fallbacks: an SPMC at S-EL1 (OP-TEE can act as one) or one at EL3 (TF-A’s EL3 SPMC hosting S-EL0 partitions such as StandaloneMM). Either gives weaker isolation.

For interrupts, the secure partition model rests on the GICv3 group split described in 4.3. Group 0 and Secure Group 1 are signalled as FIQ while Non-secure state runs, and SCR_EL3.FIQ=1 brings them to EL3 so the SPMD can hand them to the SPMC.

Checkpoint

Q12.1.1 OP-TEE at S-EL1 maps a 4 KB buffer with NS=0. The Linux driver maps the same DRAM address with its normal Non-secure mapping, and the TZASC lets Non-secure accesses through. Both sides read and write the buffer with caches enabled. What does the architecture guarantee?

  • A. Coherency, because both mappings are Inner Shareable Write-Back and refer to the same PA
  • B. Coherency, as long as both sides use DMB ISH around their accesses
  • C. Nothing: Secure PA X and Non-secure PA X are different physical locations to the caches, so the two views can diverge until explicit maintenance (and even then only through IMPLEMENTATION DEFINED aliasing in the system)
  • D. A synchronous External abort on the Non-secure access, because the PE detects the alias
Answer

C. The PAS is part of the physical address. Cache lines tagged Secure and Non-secure for the same numeric PA are unrelated locations, so the coherency protocol never connects them. A barrier orders accesses but cannot make two different locations coherent (B). The PE does not detect aliases (D). The correct design maps the shared buffer NS=1 from the Secure side as well.

Read more: Arm ARM (DDI0487), Security state and physical address spaces; Mismatched memory attributes

Q12.1.2 On a FEAT_SEL2 system with SCR_EL3.EEL2=1, an S-EL1 partition’s stage 1 descriptor has NS=1. What happens to the access?

  • A. It produces a Non-secure IPA, which is translated by the stage 2 tables at VTTBR_EL2, with the output PAS controlled by VTCR_EL2.NSA
  • B. It goes directly to the Non-secure PAS, bypassing stage 2, as it would without Secure EL2
  • C. It is translated by VSTTBR_EL2, and NS=1 is ignored because Secure stage 2 always outputs Secure PAs
  • D. It takes a stage 1 Permission fault because S-EL1 may not generate Non-secure addresses under an SPMC
Answer

A. With Secure EL2 enabled, the stage 1 NS bit selects the IPA space. Non-secure IPA space uses VTTBR_EL2/VTCR_EL2, with NSW/NSA selecting the output PAS for walks and accesses. B describes the EEL2=0 behaviour. This is also why EL3 must context-switch VTTBR_EL2: the Secure and Non-secure worlds both use it.

Read more: Arm ARM (DDI0487), VSTCR_EL2, VTCR_EL2, Secure EL2 and the Secure EL1&0 translation regime

Q12.1.3 An EL3 world-switch routine saves and restores SCTLR_EL1, TTBR0/1_EL1, TCR_EL1, MAIR_EL1, VBAR_EL1, SP_EL1, ELR_EL1 and SPSR_EL1, but not CONTEXTIDR_EL1 or TPIDR_EL1. Secure EL2 is not implemented. Which statement is correct?

  • A. This is fine: those two registers are banked by Security state in AArch64
  • B. This is fine: TLB entries are tagged by Security state, so a stale CONTEXTIDR_EL1 cannot cause harm
  • C. It is only a problem if the Secure world also uses ASIDs
  • D. It is a bug: neither register is banked, so one world’s value leaks into, and can be overwritten by, the other world
Answer

D. AArch64 EL1 system registers have a single copy shared by both Security states, so EL3 must switch everything either world uses. TPIDR_EL1 holds a per-CPU pointer in both Linux and OP-TEE, so the result is an information leak plus corruption. TLB tagging (B) protects translations, not register contents, and CONTEXTIDR_EL1 is also read by trace and debug.

Read more: Arm ARM (DDI0487), AArch64 system register descriptions (banking by Security state); TF-A cm_el1_sysregs_context_save