18.5 Writing directed architectural tests
A directed architectural test sets up a precise architectural state, performs one operation, and checks that the outcome is one the Arm ARM allows. Tests of this kind are small enough to run on RTL simulation (18.4), self-checking so that no human reads a log, and deterministic or seed-reproducible. This section builds the minimum harness: EL setup, an expected-exception framework, result signatures, multi-core synchronisation without an OS, and controlled randomisation. It then maps the same ideas onto TF-A’s test framework (TFTF).
A minimal EL3/EL2/EL1 harness
The harness owns the vectors at every EL, sets safe defaults, and drops to the EL under test. Everything not under test is either enabled or explicitly trapped, never left at its reset value, because many reset values are UNKNOWN (17.1).
.globl _start
_start:
mrs x0, MPIDR_EL1
and x0, x0, #0xffff // Aff1:Aff0; adapt to the platform
bl core_index // x0 -> linear core id in x19
adr x1, stacks_top
sub x1, x1, x19, lsl #12 // 4 KB stack per core
mov sp, x1 // (SUB shifted-register cannot write SP)
adr x0, vectors_el3
msr VBAR_EL3, x0
mov x0, #(1 << 10) | (1 << 0) // SCR_EL3: RW=1 (lower ELs AArch64), NS=1
orr x0, x0, #(1 << 8) // HCE=1: HVC enabled
orr x0, x0, #(3 << 4) // bits [5:4] are RES1
msr SCR_EL3, x0
msr CPTR_EL3, xzr // no FP/SVE/AMU traps (adjust per test)
// Write every SCTLR_ELx explicitly. Do not inherit reset values.
ldr x0, =SCTLR_EL2_TEST_VALUE
msr SCTLR_EL2, x0
isb
// Drop to EL2h with DAIF masked
mov x0, #0x3c9
msr SPSR_EL3, x0
adr x0, el2_entry
msr ELR_EL3, x0
eret
Two habits matter more than the code. First, a test states its architectural preconditions (the feature it needs from the ID registers, the EL it runs at, the memory type of each buffer) and checks them at run time. A test that is skipped because ID_AA64PFR1_EL1.MTE == 0 reports “skipped”, not “passed”. Second, the harness has a single exit path that writes a result to a location the testbench watches (below), so a test never ends in an unbounded loop.
The expected-exception framework
Most architectural tests are about exceptions: “this access must fault with this syndrome”, “this instruction must trap to EL2, not EL3”, “this one must not trap”. The framework gives the test a way to declare what it expects before running the operation, and makes the vector record what actually happened.
struct exc_record {
uint64_t esr, far, elr, spsr;
uint32_t el; /* EL the exception was taken to */
uint32_t taken; /* number of exceptions since arm */
};
struct exc_expect {
uint32_t el; /* expected target EL */
uint32_t ec; /* expected ESR_ELx.EC */
uint32_t iss_mask, iss_val;
uint64_t far; /* expected FAR, or ~0 if FAR must not be checked */
int skip; /* bytes to advance ELR on return (4 = skip the faulting insn) */
};
volatile struct exc_record exc_log[NCPU];
volatile struct exc_expect exc_exp[NCPU];
The synchronous handler at each EL saves the general-purpose registers, then:
void sync_handler(uint32_t el, uint64_t esr, uint64_t far, uint64_t elr, uint64_t spsr)
{
unsigned cpu = this_cpu();
exc_log[cpu] = (struct exc_record){ esr, far, elr, spsr, el, exc_log[cpu].taken + 1 };
if (exc_exp[cpu].ec == EXC_NONE) /* nothing expected: unexpected exception */
test_fail(cpu, FAIL_UNEXPECTED_EXC);
write_elr(el, elr + exc_exp[cpu].skip); /* step over the faulting instruction */
}
and the test reads:
expect_exception(/*el=*/2, /*ec=*/0x18, ISS_SYSREG_MASK, ISS_SYSREG(TCR2_EL1, WRITE), ~0ULL, 4);
write_tcr2_el1(0); /* from EL1, with HCRX_EL2.TCR2En == 0 */
check_exception_taken(); /* compares exc_log with exc_exp, clears both */
Rules that keep the framework honest:
- Check the target EL, and that exactly one exception was taken. A trap taken to the wrong EL is the most common category of real architectural bug, and a double fault (the handler faulting) is otherwise invisible.
- Check only architecturally fixed fields. EC, the xFSC, the fault level,
WnRand the trapped encoding are fixed.FARwhenFnV == 1or for an EC that does not write it,ELRfor an asynchronous exception, and the IMPLEMENTATION DEFINED parts of an SError syndrome (IDS == 1) are not (18.1). Masks exist for that reason. - Return via
ELR + skiponly for synchronous exceptions. For tests that expect an SError or an interrupt, the handler must return toELRunchanged, and the test must bound the wait (aDSB/ESB, a timer, or a loop count) and report “not taken” as a failure. - Keep the vector path minimal. The handler’s own memory accesses must not disturb the state under test. For cache and TLB tests, put the log in a separate Device or Non-cacheable page so that logging does not allocate lines.
Self-checking with signatures
On RTL simulation and emulation, there is no console, or it is too slow to use. Tests reduce their results to a signature: a running checksum (CRC32 or a simple multiply-xor) over every observed value, compared at the end with a golden value.
static uint32_t sig = 0x811c9dc5u;
static inline void sig_add(uint64_t v) /* FNV-1a over 8 bytes */
{
for (int i = 0; i < 8; i++) { sig ^= (uint8_t)(v >> (8 * i)); sig *= 16777619u; }
}
void test_end(void)
{
mmio_write_32(TB_MAILBOX, sig == GOLDEN_SIG ? TB_PASS : (TB_FAIL | (sig & 0xffff)));
for (;;) wfi();
}
The testbench watches TB_MAILBOX (often a magic address decoded by a simulation-only agent) and ends the run. Golden values come from running the same binary on a reference: the AEM FVP for architectural results, or a previous known-good RTL release. Only put deterministic values into the signature: syndrome fields, data values, pass/fail of each check. Timer values, cycle counts and IMPLEMENTATION DEFINED fields go in a separate log or not at all. For tests whose outcome can legally vary (ordering tests, for example), hash the set of allowed outcomes into a pass/fail bit rather than the raw outcome.
Multi-core synchronisation without an OS
Many architectural tests need several PEs to act at the same moment: a TLBI broadcast observed by another core, a store observed by a load, a message-passing pattern. The harness needs a barrier that works before any OS and, often, with caches off.
/* Sense-reversing barrier. Requires the counter to be in memory where
* atomics work for every participant: normally Inner Shareable WB Normal, MMU on. */
void cpu_barrier(volatile uint32_t *count, volatile uint32_t *sense, uint32_t n, uint32_t *local_sense)
{
*local_sense ^= 1;
if (__atomic_add_fetch(count, 1, __ATOMIC_ACQ_REL) == n) {
*count = 0;
__atomic_store_n(sense, *local_sense, __ATOMIC_RELEASE);
__asm__ volatile("dsb ish; sev" ::: "memory");
} else {
while (__atomic_load_n(sense, __ATOMIC_ACQUIRE) != *local_sense)
__asm__ volatile("wfe");
}
}
- Memory type. The barrier variables must be in memory where exclusives or LSE atomics work for all participants. That normally means the MMU is on and they are mapped Normal Inner Shareable Write-Back. With the MMU off they are Device, and whether exclusives work depends on the system (10.1, 18.2). If the test itself must run with the MMU off, synchronise through a simulation-provided hardware semaphore or through per-core flags with a single writer each, which need no atomics.
- Wake-ups.
SEVafter the releasing store, with aDSBso the store is observed before the event (10.4).WFEloops must always re-check the condition, becauseWFEcan wake spuriously. - Skew. A barrier aligns PEs to within some hundreds of cycles, which is far coarser than the window most ordering bugs need. For ordering tests, run many iterations with a small per-iteration random delay on each PE, as litmus7 does, to cover the interleavings (7.11).
Randomising addresses and attributes
Directed tests find the bug you thought of. Constrained randomisation inside a directed test finds its neighbours. Randomise only within the space the test’s precondition allows, and make every run reproducible from its seed.
- Addresses: offset within a page, which page in a pool, cache-set aliasing (addresses that map to the same set), and crossing a page or a cache line.
- Attributes:
MAIRindex (Normal WB/WT/NC, Device-nGnRE/nGnRnE), shareability, access permissions,nGand ASID, and the stage 2 attributes when EL2 is in use. Pick only legal combinations, or include mismatched attributes deliberately and check only what the architecture still guarantees (6.6). - Instruction variants: access size,
LDPvsLDR, acquire/release vs plain, exclusive vs LSE, andDCby VA vsDC ZVA. - Seeds: use a simple LFSR or xorshift generator, seeded from the testbench (a plusarg or a memory word written before reset), and write the seed into the first word of the log. A failing seed must reproduce on every platform, which rules out seeding from the timer.
Validation note: pair every directed test with its PMU expectation where one exists. A test that maps a buffer Non-cacheable and reads it should see
L1D_CACHE_REFILLstay flat. A TLBI test should seeDTLB_WALKincrease on the next access (16.5). If the PMU contradicts the architectural result, the test may be passing for the wrong reason: for example, a cache test that never allocated a line.
The TF-A test framework (TFTF)
TF-A Tests (the tf-a-tests repository) is the open source suite for TF-A itself. It is useful both as a test suite and as a reference harness.
- What it is. TFTF runs as the Non-secure BL33 image, at NS-EL2 (or NS-EL1), on top of a real TF-A build. It tests the firmware interfaces from the normal world: PSCI (CPU_ON/OFF/SUSPEND, system suspend), SMCCC and the
SMCCC_ARCH_WORKAROUND_*calls, SDEI, FF-A and the SPM, the TRNG and Errata ABIs, RAS and EA handling, and the EL3 feature enables, for example tests that access a newly enabled register and expect no trap. Secure partitions for SPM tests (Cactus, Ivy) and a Realm payload for RME are built from the same tree. - Test lists. Tests are grouped into XML test lists (
tftf/tests/tests-*.xml), and a build selects one withTESTS=.make help_testslists the sets, and the default is the standard set. - Multi-core and reboot. TFTF has its own CPU-on, barrier and timer services, and it can reboot the platform between tests, keeping state in non-volatile storage. It is a worked example of the synchronisation and expected-result patterns above.
# Build TFTF for FVP with the standard tests, then use it as BL33
cd tf-a-tests
make PLAT=fvp TESTS=standard tftf
cd ../trusted-firmware-a
make PLAT=fvp BL33=../tf-a-tests/build/fvp/release/tftf.bin all fip
TFTF tests the firmware against the architecture and the firmware specifications. It is not a CPU architecture compliance suite. For a new platform port, getting the standard TFTF set to pass on FVP, then on emulation, is a strong check that PSCI, the interrupt handling and the feature enables are wired correctly before any OS is involved.
Checkpoint
Q18.5.1 A directed test expects MSR TCR2_EL1, x0 at EL1 to trap to EL2 with EC 0x18. The exception log shows one exception, EC 0x18, with the correct ISS, taken to EL3. What should the test report?
- A. Pass, because the EC and ISS match
- B. Pass, because the target EL is IMPLEMENTATION DEFINED
- C. Skip, because EL3 interfered
- D. Fail: the target EL is part of the expected result, and a trap to EL3 means an EL3 control (
SCR_EL3.TCR2En) is trapping, so the test precondition or the implementation is wrong
Answer
D. Which EL a trap is taken to is architecturally determined by which control causes it. A test that ignores the target EL misses exactly the class of bug it was written for. The harness should have set SCR_EL3.TCR2En = 1 as a precondition, and if it did, the implementation is wrong. B is false: the trap priority and routing are defined by the architecture.
Read more: Arm ARM (DDI0487), TCR2_EL1 accessibility pseudocode; SCR_EL3.TCR2En; HCRX_EL2.TCR2En
Q18.5.2 Which value is safe to include in a test’s golden signature?
- A.
FAR_EL1after a synchronous External abort withFnV == 1 - B. The DFSC and
WnRof a translation fault on a store to an unmapped page - C.
CNTPCT_EL0at the end of the test - D. The ISS of an SError taken to EL1 with
ESR_EL1.IDS == 1
Answer
B. The fault status code, level and direction of a translation fault are architecturally fixed. A is UNKNOWN by definition. C is timing-dependent. D is the tempting one, because it is still an ESR_EL1 value, but with IDS == 1 the rest of the ISS is IMPLEMENTATION DEFINED, so the golden value would differ between the reference model and the implementation under test.
Read more: Arm ARM (DDI0487), ISS encoding for an exception from a Data Abort; ISS encoding for an SError exception
Q18.5.3 A four-core test must synchronise before the MMU is enabled. Which approach is safe on an arbitrary system?
- A. An
LDAXR/STLXRcounter in DRAM, since exclusives always work on DRAM - B. An
LDADDcounter in DRAM, since LSE atomics do not need a monitor - C. One flag per core, each written only by its owner and polled by the others, with a
DSBandSEVafter each write - D. No synchronisation: cores leave reset at the same cycle
Answer
C. With the MMU off, data accesses are Device-nGnRnE. Exclusives (A) then depend on a global monitor in the system, and far atomics (B) depend on the target supporting them, neither of which is guaranteed. Single-writer flags need only ordinary stores and loads. D is false on any real system, where reset release is staggered.
Read more: Arm ARM (DDI0487), Exclusive access instructions and Shareable memory locations; Atomic instructions