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

16.6 Profiling and trace

Four self-hosted mechanisms record what a PE did without halting it. The PMU (16.3) counts. The three covered here record samples, program flow or recent branches. Two of them (SPE, and ETE when its output goes through TRBE) write into memory buffers that a lower EL owns. BRBE keeps its records in System registers:

MechanismFEAT_RecordsOutputOwner control at EL3
Statistical Profiling (SPE)FEAT_SPE, permitted from v8.1one sampled operation: PC, VA/PA, latencies, events, data sourceProfiling Buffer in memoryMDCR_EL3.NSPB/NSPBE
Embedded Trace (ETE)FEAT_ETE, permitted from v9.0the full program flow (waypoints), with optional timestamps and contexttrace sink, or TRBEMDCR_EL3.STE/TTRF, CPTR_EL3.TTA
Trace Buffer (TRBE)FEAT_TRBE, permitted from v9.0(ETE’s output)Trace Buffer in memoryMDCR_EL3.NSTB/NSTBE
Branch Record Buffer (BRBE)FEAT_BRBE, permitted from v9.1the last n taken branches and exceptionssystem registersMDCR_EL3.SBRBE

From the firmware point of view, the same three questions apply to each. Who owns it? What can leak across a Security state boundary? What must be drained or saved when the owner changes? The bit-by-bit EL3 enable table is in 17.3, and the external authentication signals (NIDEN, SPNIDEN, RLPIDEN) are in 16.2.

SPE

SPE selects one operation every interval (with optional random perturbation), follows it through the pipeline, and writes a record of packets to the Profiling Buffer when it completes.

RegisterRole
PMSCR_EL1 / PMSCR_EL2enables per EL (E0SPE, E1SPE, E0HSPE, E2SPE). PA collects physical addresses, TS timestamps, CX the CONTEXTIDR
PMSIRR_EL1sampling INTERVAL and RND
PMSICR_EL1the internal interval counter, saved and restored on a context switch
PMSFCR_EL1, PMSEVFR_EL1, PMSLATFR_EL1filtering by operation type (B, LD, ST), by events (for example “L1D refill” or “TLB walk”) and by minimum latency
PMBLIMITR_EL1buffer LIMIT, enable E, fill mode FM
PMBPTR_EL1write pointer (a VA in the owning EL’s translation regime)
PMBSR_EL1buffer management status: EC (buffer full, stage 1 or stage 2 fault, GPC fault), S (service), COLL (collision), DL (data lost)
PMSIDR_EL1, PMBIDR_EL1features. PMBIDR_EL1.P = 1 means the buffer is unusable at this EL because of ownership

A buffer management event (buffer full, or a fault on a buffer write) stops collection and asserts the Profiling Buffer management interrupt, a PPI. PSB CSYNC (Profiling Synchronization Barrier) makes sure that all sampled operations before it have written their records, so it is required before reading the buffer and before changing who owns it. It is followed by a DSB so that the buffer writes complete.

Version deltas:

FeatureVersion (per the Arm feature list)Adds
FEAT_SPEv1p1permitted from v8.2alignment-flag event and filter; SVE operation profiling
FEAT_SPEv1p2permitted from v8.6 (v8.7 feature)freeze the PMU on a buffer management event (PMBLIMITR_EL1.PMFZ); discard mode (FM). The separately optional FEAT_SPE_FnE, which requires v1p2, adds the inverted event filter PMSNEVFR_EL1, trapped by MDCR_EL3.EnPMSN
FEAT_SPEv1p3permitted from v8.7 (v8.8 feature)sampling of MTE tag operations and of CPY*/SET* (FEAT_MOPS)
FEAT_SPEv1p4permitted from v8.8 (v8.9 feature)extra data-source information in the Events packet and matching filter bits; data-source filtering (FEAT_SPE_FDS, PMSDSFR_EL1) is a companion feature

v9.5 adds further optional pieces (FEAT_SPE_EXC, which reports buffer events as profiling exceptions, and FEAT_SPE_nVM, which allows a physically addressed buffer). Each new control register comes with its own MDCR_EL3 enable bit (EnPMS3, EnPMS4 and so on), and an EL3 that does not know about one leaves the lower EL trapping.

Security note: with PMSCR_ELx.PA == 1 SPE records physical addresses and data-source information (which cache level or which remote die satisfied a load). That is a strong side channel if the records cover another world’s execution, so EL3 must ensure that SPE never samples in a Security state that does not own the buffer. The ownership encoding does this: profiling is disabled in every Security state that does not own the Profiling Buffer.

ETE, TRBE and FEAT_TRF

ETE is the Armv9 trace unit, and in v9 any PE trace unit must be ETE. It is programmed through TRC* System registers (TRCPRGCTLR, TRCCONFIGR, address comparators and so on), and those accesses are trapped by CPTR_EL3.TTA, CPTR_EL2.TTA and CPACR_EL1.TTA. Its output goes either to an external CoreSight trace sink (ETR or ETF over ATB, see 16.2) or to TRBE, a per-PE unit that writes trace into memory:

RegisterRole
TRBLIMITR_EL1LIMIT, enable E, fill mode FM (fill, wrap, circular), trigger mode TM, nVM (physically addressed buffer)
TRBPTR_EL1, TRBBASER_EL1write pointer and base
TRBSR_EL1management status (EC, IRQ, TRG, WRAP, S, and the buffer or fault status code)
TRBMAR_EL1memory attributes for trace writes
TRBTRG_EL1trigger counter
TRBIDR_EL1features. P = 1 means owned by another EL or state, so programming is not allowed

FEAT_TRF (permitted from v8.3, the v8.4 feature) adds self-hosted trace filtering. TRFCR_EL1 (E0TRE, E1TRE) and TRFCR_EL2 (E0HTRE, E2TRE, CX) prohibit trace generation per EL. TS selects the timestamp (virtual, guest-physical or physical). TSB CSYNC (Trace Synchronization Barrier) orders trace operations against later System register writes. EL3’s controls are MDCR_EL3.STE (when 0, trace is prohibited in Secure state unless the IMPLEMENTATION DEFINED authentication interface overrides it), MDCR_EL3.RLTE for Realm state with RME, and MDCR_EL3.TTRF to trap TRFCR_ELx accesses.

Trace is prohibited by default at EL3, and in Secure state if external authentication does not permit it. A prohibited region produces no trace. It is not hidden later: it is never generated.

BRBE

BRBE keeps the last n taken branches (BRBIDR0_EL1.NUMREC) in three register arrays, readable in banks of 32 selected by BRBFCR_EL1.BANK:

  • BRBSRC<n>_EL1: source address;
  • BRBTGT<n>_EL1: target address;
  • BRBINF<n>_EL1: TYPE (direct, indirect, call, return, ERET, and exception types such as IRQ, SError or trap), the source and target EL, misprediction (MPRED), and an elapsed-cycle count (CC_EXP/CC_MANT).

Controls: BRBCR_EL1/BRBCR_EL2 enable recording per EL (E0BRE, E1BRE, E0HBRE, E2BRE) and select what is captured (CC, MPRED, EXCEPTION, ERTN, TS). FZP freezes recording when a PMU counter overflows, which makes BRBE the “what led to this PMU interrupt” record. BRBFCR_EL1 filters branch types and shows PAUSED. BRB IALL invalidates all records, and BRB INJ injects one from BRBINFINJ_EL1/BRBSRCINJ_EL1/BRBTGTINJ_EL1, which a hypervisor uses to restore a guest’s buffer.

MDCR_EL3.SBRBE [33:32]:

SBRBEAccesses from EL0–EL2Prohibited regions
0b00trapped to EL3 in all Security statesSecure and Non-secure EL0–EL2
0b01trapped to EL3 in Secure state onlySecure state
0b10trapped to EL3 in all statesnone
0b11not trappednone

TF-A programs 0b01. Non-secure software uses BRBE, Secure state neither records nor accesses it, and so a Non-secure reader cannot obtain Secure branch addresses. EL3 itself does not record by default. FEAT_BRBEv1p1 (permitted from v9.2) adds MDCR_EL3.E3BREC/E3BREW, which together enable EL3 recording.

What EL3 must enable and context-switch

TF-A builds per-world MDCR_EL3/CPTR_EL3 values (ENABLE_SPE_FOR_NS, ENABLE_TRBE_FOR_NS, ENABLE_TRF_FOR_NS, ENABLE_SYS_REG_TRACE_FOR_NS, ENABLE_BRBE_FOR_NS). It switches ownership on a world switch rather than saving the units’ contents:

UnitNon-secure world valueSecure/Realm world valueDrain on EL3 entry
SPENSPB = 0b11 (NS owns, NS EL1/EL2 may program), NSPBE = 0, enable bits for the newer PMS* registers setNSPB = 0b10 (NS still owns the buffer, so profiling is disabled in Secure and Realm, and their accesses trap)PSB CSYNC
TRBENSTB = 0b11, NSTBE = 0NSTB = 0b10TSB CSYNC
ETE/TRFCPTR_EL3.TTA = 0; MDCR_EL3.STE = 0, TTRF = 0same (trace prohibited in Secure by STE)TSB CSYNC
BRBESBRBE = 0b01samenone needed (no memory buffer); ISB

The MDCR_EL3 encodings follow one pattern: in NSPB/NSTB, bit [1] selects the owning Security state (1 = Non-secure) and bit [0] allows the owner’s EL1/EL2 to access the controls. With RME, NSPBE/NSTBE = 1 makes Realm the owner.

TF-A’s EL3 entry path executes psb csync and tsb csync (when built with SPE/TRBE support) followed by isb, before saving the lower EL context. This makes sure that profiling and trace generated by the Non-secure code are written to memory before the world changes. Without the barriers, a buffer write could complete after the switch, or be attributed to the next owner.

The lower-EL control registers belong to the EL1/EL2 context and must be part of TF-A’s per-world EL1/EL2 save and restore when a Secure-EL1/EL2 exists: TRFCR_EL1, TRFCR_EL2, PMSCR_EL1/PMSCR_EL2, BRBCR_EL1/BRBCR_EL2. Otherwise a Non-secure “trace EL1” setting persists into S-EL1, and only the STE/ownership bits stop it from having effect. The buffer contents are not switched: Secure software cannot touch them while it does not own them.

When EL2 is not used by the Non-secure world, EL3 must also set up MDCR_EL2 so that EL1 owns the buffers (E2PB, E2TB) and EL1 accesses are not trapped (TPMS, TTRF). Otherwise Linux at EL1 sees SPE and TRBE in the ID registers but every access traps.

Bring-up note: “perf record with SPE or TRBE works on one core and not another” is usually an ownership problem. Read PMBIDR_EL1.P and TRBIDR_EL1.P at the OS EL: if either is 1, EL3 or EL2 left ownership elsewhere. Some cores also have errata that require TRBE to be disabled entirely or the buffer to be drained differently (TF-A has a per-core check for this), so check the errata notice before trusting trace on a new core revision (1.3).

Validation note: a minimal leak test for each world switch. Enable SPE with PA = 1 and the sampling interval at its minimum, run an SMC-heavy loop that enters a Secure payload, then parse the buffer. No record may have a PC in the Secure image’s address range or carry a Secure-state context. Do the same with ETE/TRBE (no trace packets between the SMC and the return) and with BRBE (no records with a Secure source or target address. The exception record for the SMC itself is still written, but because EL3 is a prohibited region its target address is not recorded). A failure means an MDCR_EL3 value per world is wrong, or that the barrier on the entry path is missing. PMBSR_EL1.DL (data lost) and an unexpected TRBSR_EL1.S (stopped) must not appear in normal operation.

Checkpoint

Q16.6.1 A Non-secure kernel is profiling with SPE when an SMC enters EL3, and EL3 switches MDCR_EL3.NSPB from 0b11 to 0b10 before entering the Secure payload. What is the purpose of the PSB CSYNC that TF-A executes on EL3 entry?

  • A. It disables SPE so that EL3 is not sampled
  • B. It ensures that records for operations sampled before it have been written to the Profiling Buffer, so they cannot complete after the change of world or ownership
  • C. It saves PMSICR_EL1 so that sampling resumes at the same interval
  • D. It invalidates the Profiling Buffer so that Secure data cannot be read
Answer

B. PSB CSYNC is the profiling synchronization barrier. Together with the following DSB/ISB it makes sure that pending sample records from before the barrier are written out. A is wrong because EL3 is excluded from sampling by its own rules, and in Secure/Realm state the ownership encoding is what disables profiling. Nothing saves registers (C), and nothing is invalidated (D).

Read more: Arm ARM (DDI0487), PSB CSYNC; MDCR_EL3.NSPB; The Statistical Profiling Extension, synchronization

Q16.6.2 Firmware sets MDCR_EL3.SBRBE = 0b11 instead of 0b01. Which new risk does this create?

  • A. Non-secure EL1 can no longer read BRBE records
  • B. Branches executed at EL3 are recorded
  • C. BRBE register accesses from the Non-secure world trap to EL3
  • D. Branches executed in Secure EL0–EL2 can be recorded, and the records can then be read by Non-secure software, exposing Secure control flow and addresses
Answer

D. 0b11 removes both the Secure prohibited region and the Secure trap, while the record buffer is shared by all Security states. A and C are the opposite of what 0b11 does. EL3 recording (B) is controlled separately by E3BREC/E3BREW (FEAT_BRBEv1p1) and stays disabled.

Read more: Arm ARM (DDI0487), MDCR_EL3.SBRBE; The Branch Record Buffer Extension, prohibited regions

Q16.6.3 On a new platform, Linux at Non-secure EL1 (no hypervisor) detects TRBE but every TRBLIMITR_EL1 write traps. MDCR_EL3.NSTB is 0b11. What is the most likely cause?

  • A. MDCR_EL2.E2TB selects EL2 as the owner of the trace buffer, because EL3 did not initialise MDCR_EL2 for an unused EL2
  • B. MDCR_EL3.STE is 0
  • C. TRFCR_EL1.E1TRE is 0
  • D. CPTR_EL3.TTA is 1
Answer

A. With NSTB = 0b11 the Non-secure state owns the buffer and EL3 is not trapping. The remaining owner choice is MDCR_EL2.E2TB (EL1 or EL2), which EL3 must program when EL2 is unused. STE (B) concerns Secure-state trace generation. E1TRE (C) prohibits generation, not register access. CPTR_EL3.TTA (D) traps the ETE TRC* registers, not the TRBE buffer registers.

Read more: Arm ARM (DDI0487), MDCR_EL2.E2TB; MDCR_EL3.NSTB; TRBIDR_EL1.P