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

5.9 Lab: debugging translation with AT and PAR_EL1

The address translation (AT) instructions ask the MMU “what would happen if this regime accessed this VA now?” The answer is written to PAR_EL1 instead of being delivered as a fault. They are the most precise tool for translation bugs, because they report what the hardware does, including its TLB contents and walk attributes, rather than what your table dump claims. This lab uses them from EL3 to dry-run page tables, and reproduces the five commonest descriptor mistakes.

The AT operations

All of them take a VA in Xt, write PAR_EL1, and are encoded as SYS instructions.

InstructionLowest ELRegime and stagesPermissions checked as
AT S1E0R, AT S1E0WEL1EL1&0 stage 1an EL0 read/write
AT S1E1R, AT S1E1WEL1EL1&0 stage 1an EL1 read/write, ignoring PSTATE.PAN
AT S1E1RP, AT S1E1WPEL1EL1&0 stage 1an EL1 read/write, honouring PSTATE.PAN (FEAT_PAN2: OPTIONAL from v8.1, mandatory from v8.2)
AT S1E2R, AT S1E2WEL2EL2 or EL2&0 stage 1an EL2 read/write
AT S12E0R, AT S12E0WEL2EL1&0 stage 1 and stage 2EL0
AT S12E1R, AT S12E1WEL2EL1&0 stage 1 and stage 2EL1
AT S1E3R, AT S1E3WEL3EL3 stage 1EL3
AT S1E1A, AT S1E2A, AT S1E3AEL1/EL2/EL3stage 1 of that regimeno permission check (FEAT_ATS1A, Armv8.9 extension, OPTIONAL from v8.8, ID_AA64ISAR2_EL1.ATS1A)

Behaviours that matter when you read the result:

  • Stage 1 only versus stage 1+2. S1E0/S1E1 in a regime with stage 2 enabled return the IPA. S12E0/S12E1 return the final PA with combined attributes. If stage 2 is disabled, S12 behaves like S1.
  • Stage 2 faults during a stage 1-only AT. The stage 1 walk’s own table reads still go through stage 2. When AT S1E1R executed at EL1 hits a stage 2 fault on a table read, that fault is taken to EL2 as a Data Abort with S1PTW = 1 and CM = 1, not recorded in PAR_EL1. When executed at EL2 or EL3, it is recorded in PAR with S = 1.
  • From EL3, the Security state comes from SCR_EL3. AT S1E1*/S12E1* at EL3 translate in the EL1&0 regime of the state selected by SCR_EL3.NS (and NSE with RME), using the EL1/EL2 system registers as they currently hold. They are only meaningful while those registers contain that world’s context.
  • AT reflects TLB state. An AT may be satisfied from cached TLB entries, so after a descriptor change without the TLBI it can return the old translation, just as a real access would. If AT disagrees with your manual walk of memory, suspect missing maintenance (9.1). Some models always walk memory, so they cannot show this.
  • AT is not a load. It does not raise stage 1 faults, but it can raise synchronous External aborts on walk reads. With hardware AF management enabled it may set AF.

FEAT_ATS1A exists because a debugger or hypervisor often needs the PA of a mapping it has no permission to access, for example an execute-only page or a page behind a POE overlay. Before it, software had to try R, then W, then reason about why both failed.

PAR_EL1 formats

PAR_EL1.F (bit 0) selects the layout. The 64-bit format:

BitsF = 0 (success)F = 1 (fault)
[63:56]ATTR: resulting memory attributes in MAIR encodingIMPLEMENTATION DEFINED
[55:52]RES0IMPLEMENTATION DEFINED
[51:12]PA[51:12] (bits [51:48] with FEAT_LPA/LPA2)RES0
[15] / [14] / [13] / [12](part of PA)DirtyBit (PIE) / Overlay (POE) / TopLevel / AssuredOnly (FEAT_THE)
[11]NSE (RME)RES1
[10]IMPLEMENTATION DEFINEDRES0
[9]NSS: fault was at stage 2
[8:7]SH: resulting shareability[8] PTW: stage 2 fault on a stage 1 walk
[6:1]RES0FST: fault status, same encoding as DFSC (5.8)
[0]F = 0F = 1

PA is page-granular. Add VA[11:0] (for 4K) yourself. NS (and NSE) report the output physical address space for regimes where that is defined, and are UNKNOWN in some others (for example NS for a Non-secure regime). With FEAT_D128 the register is 128 bits wide. When PAR_EL1.D128 (bit [64]) is 1, a successful result holds PA = OA[55:12] in bits [119:76], with ATTR, SH, NS and NSE where the 64-bit format has them. A fault result keeps the 64-bit fault layout in bits [63:0]. Read it with MRRS (5.7).

The ISB before reading PAR

AT writes PAR_EL1 as a side effect. That is an indirect write of a system register, and a direct read (MRS) is only guaranteed to observe it after a context synchronization event. So the sequence is always:

    at      s1e1r, x0
    isb                     // without this, MRS may return the previous PAR value
    mrs     x0, par_el1

The ISB rules are in 7.8. An ISB is also needed before the AT if you have just written TTBR/TCR/MAIR/SCTLR, so that the AT uses the new values. Some cores add errata on top. Linux’s read_sysreg_par() inserts a DMB SY around the PAR read for Cortex-A77 erratum 1508412.

Security note: PAR_EL1 belongs to the EL1 context. TF-A saves and restores it per world for this reason. EL3 debug or RAS code that runs AT and then returns to the same world without a context restore has overwritten that world’s PAR_EL1. That breaks any in-flight AT/MRS sequence in the lower EL, and in the other direction it can expose a translation result across worlds. Save PAR_EL1 before using AT at EL3 and restore it afterwards.

Using AT from a debugger

An external debugger can execute AT in Debug state through the ITR (EDITR) and then read PAR_EL1. Arm Development Studio’s MMU view and most JTAG tools use this, or a walk of the tables in memory, or both. Know which one your tool uses. A table-reading walk shows memory contents. An AT shows what the MMU does, including stale TLB entries and the walk’s cacheability. When they disagree, the difference is the bug.

QEMU’s gdbstub cannot execute instructions for you, but the HMP monitor (reachable from gdb as monitor <cmd>) provides:

  • monitor gva2gpa <va>, which translates using the current CPU’s current regime (stage 1 and 2);
  • monitor xp /4gx <pa>, which dumps descriptors by physical address;
  • -d int on the command line, which prints ESR, FAR and ELR for every exception taken.

QEMU has no info tlb or info mmu monitor command for Arm targets (those are x86/PPC/SPARC-only), and -d mmu only logs PMSA (M- and R-profile MPU) lookups, not VMSA walks. On A-profile, use gva2gpa, xp, and AT in your own code.

Lab

Everything runs at EL3 on QEMU virt or FVP Base RevC. The trick is to dry-run the tables in the Secure EL1&0 regime. start.S first sets SCR_EL3.RW = 1. Without it EL1 is AArch32, and AT S1E1R from EL3 would translate with the AArch32 regime’s formats. EL3 then writes TTBR0_EL1, TCR_EL1, MAIR_EL1 and SCTLR_EL1.M = 1 (nothing runs at EL1) and probes with AT S1E1R. A broken table produces a PAR value, not a dead EL3. The same technique validates real firmware tables before the MSR SCTLR_EL3 that would make the mistake fatal.

Files are in labs/part05/ (see Lab setup for toolchains).

start.S

    .section .text.boot, "ax"
    .global _start
_start:
    mov     x0, #0x430          // SCR_EL3: RW=1 (EL1 is AArch64), RES1 [5:4], NS=0 (Secure)
    msr     scr_el3, x0
    msr     hcr_el2, xzr        // matters only for extension 1 (NS=1); reset value may be UNKNOWN
    isb
    ldr     x0, =__stack_top
    mov     sp, x0
    bl      main
1:  wfe
    b       1b

    .text
    .global at_s1e1r
at_s1e1r:                       // uint64_t at_s1e1r(uint64_t va)
    at      s1e1r, x0
    isb
    mrs     x0, par_el1
    ret

    .global el1_regime_install  // (ttbr0, tcr, mair)
el1_regime_install:
    msr     mair_el1, x2
    msr     tcr_el1, x1
    msr     ttbr0_el1, x0
    ldr     x3, =0x30d00801     // SCTLR_EL1: RES1 bits + M=1 (C=0, I=0)
    msr     sctlr_el1, x3
    dsb     ish                 // descriptor stores visible to the walker
    tlbi    alle1               // drop any cached EL1&0 entries (SCR_EL3.NS selects the state)
    dsb     ish
    isb                         // new EL1 regime visible to subsequent AT
    ret

lab.c

#include <stdint.h>

#ifdef PLAT_FVP
#define UART_BASE 0x1c090000UL
#define DRAM_BASE 0x80000000UL
#else                                   /* QEMU virt */
#define UART_BASE 0x09000000UL
#define DRAM_BASE 0x40000000UL
#endif

#define BLK     0x1ULL                  /* block descriptor, bits[1:0] = 01 */
#define TBL     0x3ULL                  /* table descriptor, bits[1:0] = 11 */
#define AF      (1ULL << 10)
#define SH_IS   (3ULL << 8)
#define IDX(n)  ((uint64_t)(n) << 2)
enum { MT_NORMAL = 0, MT_DEVICE = 1 };
#define MAIR    0x00000000000000ffULL   /* Attr0 = 0xFF Normal WB, Attr1 = 0x00 Device-nGnRnE */
/* T0SZ=32 (4GB, start level 1), IRGN0/ORGN0=WBWA, SH0=IS, TG0=4K, EPD1=1, TG1=4K, IPS=40-bit */
#define TCR     0x280803520ULL

static uint64_t l1[512] __attribute__((aligned(4096)));
static uint64_t l2[512] __attribute__((aligned(4096)));
extern uint64_t at_s1e1r(uint64_t va);
extern void el1_regime_install(uint64_t ttbr, uint64_t tcr, uint64_t mair);

/* -nostdlib: GCC may still emit calls to these for aggregate initialisers such as c[] below */
void *memcpy(void *d, const void *s, __SIZE_TYPE__ n) { char *p = d; const char *q = s; while (n--) *p++ = *q++; return d; }
void *memset(void *d, int v, __SIZE_TYPE__ n) { char *p = d; while (n--) *p++ = (char)v; return d; }

static void putc_(char c) { *(volatile uint32_t *)UART_BASE = c; }
static void puts_(const char *s) { while (*s) putc_(*s++); }
static void hex(uint64_t v) { puts_("0x"); for (int i = 60; i >= 0; i -= 4) putc_("0123456789abcdef"[(v >> i) & 0xf]); }

static void probe(const char *what, uint64_t va)
{
    uint64_t par = at_s1e1r(va);
    puts_(what); puts_(" va="); hex(va);
    if (par & 1) {
        puts_("  FAULT fst="); hex((par >> 1) & 0x3f);
        puts_(" ptw="); putc_('0' + ((par >> 8) & 1));
        puts_(" s="); putc_('0' + ((par >> 9) & 1));
    } else {
        puts_("  pa="); hex((par & 0x000ffffffffff000ULL) | (va & 0xfff));
        puts_(" attr="); hex(par >> 56);
        puts_(" sh="); putc_('0' + ((par >> 7) & 3));
    }
    puts_("\n");
}

int main(void)
{
    const uint64_t probe_va = DRAM_BASE + 0x20000000;        /* level 2 index 256 */
    const uint64_t good = probe_va | BLK | IDX(MT_NORMAL) | SH_IS | AF;
    struct { const char *name; uint64_t l2e; uint64_t tcr; } c[] = {
        { "good        ", good,                                   TCR },
        { "1 AttrIndx  ", (good & ~IDX(7)) | IDX(MT_DEVICE),      TCR },
        { "2 no AF     ", good & ~AF,                             TCR },
        { "3 misaligned", good | 0x100000,                        TCR },
        { "4 SH=NSH    ", good & ~SH_IS,                          TCR },
        { "5 T0SZ=16   ", good,                       (TCR & ~0x3fULL) | 16 },
    };

    for (int i = 0; i < 512; i++) { l1[i] = 0; l2[i] = 0; }
    l1[0] = 0x0 | BLK | IDX(MT_DEVICE) | AF;                  /* first GB: MMIO */
    l1[DRAM_BASE >> 30] = (uint64_t)(uintptr_t)l2 | TBL;
    for (int i = 0; i < 512; i++)
        l2[i] = (DRAM_BASE + ((uint64_t)i << 21)) | BLK | IDX(MT_NORMAL) | SH_IS | AF;

    for (unsigned k = 0; k < sizeof c / sizeof c[0]; k++) {
        l2[256] = c[k].l2e;
        el1_regime_install((uint64_t)(uintptr_t)l1, c[k].tcr, MAIR);
        probe(c[k].name, probe_va + 0x1234);
    }
    el1_regime_install((uint64_t)(uintptr_t)l1, TCR, MAIR);
    probe("unmapped GB ", 0xc0000000);                        /* l1[3] == 0 on QEMU */
    probe("beyond T0SZ ", 0x100000000ULL);                    /* VA[63:32] != 0 */
    return 0;
}

link.ld (set LOAD_ADDR to 0x40080000 for QEMU or 0x80080000 for FVP):

ENTRY(_start)
SECTIONS {
  . = LOAD_ADDR;
  .text   : { *(.text.boot) *(.text*) }
  .rodata : { *(.rodata*) }
  .data   : { *(.data*) }
  .bss    : ALIGN(4096) { *(.bss*) *(COMMON) }
  . = ALIGN(16); . += 0x4000; __stack_top = .;
}

Build and run on QEMU:

CFLAGS="-O1 -ffreestanding -nostdlib -mstrict-align -mgeneral-regs-only -fno-tree-loop-distribute-patterns"
aarch64-none-elf-gcc $CFLAGS -Wl,--defsym=LOAD_ADDR=0x40080000 -T link.ld start.S lab.c -o lab.elf
qemu-system-aarch64 -M virt,secure=on,virtualization=on -cpu max -m 2G -nographic \
    -kernel lab.elf -d int,guest_errors -D qemu.log        # exit: Ctrl-A x

A bare-metal ELF on virt with secure=on starts at EL3 at its entry point. -mstrict-align is not optional here: with SCTLR_EL3.M = 0, EL3’s own data accesses (stack included) are Device-nGnRnE, and an unaligned compiler-generated access would be an Alignment fault (6.5).

On FVP (needs the Arm FVP download; paths vary by version):

aarch64-none-elf-gcc $CFLAGS -DPLAT_FVP -Wl,--defsym=LOAD_ADDR=0x80080000 -T link.ld start.S lab.c -o lab_fvp.elf
FVP_Base_RevC-2xAEMvA -C cluster0.NUM_CORES=1 \
    -C bp.secure_memory=0 -C bp.pl011_uart0.out_file=- -C bp.vis.disable_visualisation=1 \
    -a cluster0.cpu0=lab_fvp.elf \
    --plugin "$PVLIB_HOME"/plugins/Linux64_GCC-*/TarmacTrace.so \
    -C TRACE.TarmacTrace.trace-file=lab.tarmac

If your model version does not start at the ELF entry, set the reset address (--list-params | grep -i rvbar). To find the Tarmac switches for translation-walk and TLB records in your version, run --plugin .../TarmacTrace.so --list-params | grep -i -E 'mmu|tlb|walk'. The parameter names have changed between releases. Even without walk records, the trace shows each AT instruction followed by the PAR_EL1 register write, which is enough to check results. Remember that FVP does not model cache contents by default, so it cannot show stale-table bugs (18.4).

What you should see, and why

CaseExpected PAR_EL1What it means
goodF=0, pa = DRAM_BASE+0x2000_1234, attr = 0xff, sh = 3Baseline
1 wrong AttrIndxF=0, attr = 0x00, sh = 2 (Device memory is always reported as Outer Shareable)The translation succeeds, so nothing faults until much later: alignment faults on unaligned accesses, DC ZVA faults, exclusives that never succeed, and very slow memcpy. Always check ATTR, not just F
2 missing AFF=1, fst = 0x0a (Access flag, level 2)The level in FST is the level of the leaf. With TCR_EL1.HA = 1 the AT may instead set AF and succeed (5.6)
3 block misalignmentUsually F=0 with pa still DRAM_BASE+0x2000_1234Bits [20:12] of a level 2 block OA are RES0. Writing them gives no reliable result, and most implementations ignore them, so the mapping silently points somewhere other than the code intended. Compare pa with the intended PA, not with the descriptor
4 SH mismatchF=0, sh = 0Fine on a single core. On silicon, other cores that map the same PA as Inner Shareable lose coherency with this one (6.4, 6.6). With DS == 1, shareability comes from TCR, not the descriptor (5.7)
5 T0SZ/start-level mismatchF=1, fst = 0x04 (Translation, level 0)T0SZ = 16 starts the walk at level 0, so l1 is read as a level 0 table. Its entry 0 is a block descriptor, which is invalid at level 0 for 4K without LPA2. The reverse mistake (tables built from level 0, T0SZ implying level 1) makes the walker use a table descriptor as if it were one level lower, and usually ends in a fault or a wildly wrong pa
unmapped GBF=1, fst = 0x05Invalid level 1 descriptor
beyond T0SZF=1, fst = 0x04A VA outside the TxSZ range is reported as a level 0 Translation fault whatever the start level

DDI0487 permits PAR_EL1.ATTR and SH to report the attributes the implementation actually uses rather than the ones the descriptor and MAIR asked for. A core may therefore show a different Normal encoding than 0xff in the good case. Compare the class (Normal Write-Back, Non-cacheable or Device), not the exact byte.

Note the starting-level boundaries for 4K: T0SZ 16–24 start at level 0, 25–33 at level 1, and 34–39 at level 2 (5.3). Changing T0SZ from 32 to 25 would not break these tables. Changing it to 24 would.

Extensions:

  1. Set SCR_EL3.NS = 1 before el1_regime_install and rerun. You are now probing the Non-secure EL1&0 regime. Then set descriptor bit 5 (NS) in the Secure case and watch PAR.NS.
  2. Remove the tlbi alle1 and change l2[256] between two probes. On QEMU the AT still sees the new descriptor, because it walks memory every time. On silicon it may not, and that difference is the reason the TLBI is in the sequence.
  3. If ID_AA64ISAR2_EL1.ATS1A and ID_AA64MMFR3_EL1.S1PIE are non-zero, enable PIE (TCR2_EL1.PIE, which needs SCR_EL3.TCR2En and SCR_EL3.PIEn). Program a PIR_EL1 entry that gives EL1 no read access (for example execute-only), point the probe page at it, and compare AT S1E1R (Permission fault, fst = 0x0e) with AT S1E1A (success). This needs an assembler that knows FEAT_ATS1A, for example via -march=armv8.9-a. The PIE encodings are in 5.5.

Validation note: AT is also a target of validation, not just a tool. A directed test should check that AT agrees with real accesses for every fault class (provoke the same VA with a load, compare ESR.DFSC with PAR.FST), that S1E1RP honours PSTATE.PAN while S1E1R does not, that PAR.ATTR/SH equal the combined stage 1 and stage 2 attributes for S12E1R, and that a missing ISB is the only way to see a stale PAR. Disagreement between AT and a real access usually points to the walker’s two code paths (the one that serves AT and the one that serves loads) having diverged in the RTL or the model.

Checkpoint

Q5.9.1 EL3 firmware builds tables for its own regime and, before setting SCTLR_EL3.M, runs AT S1E3R on the PC to check them. PAR_EL1 reports success with PA == VA, even for an address the tables do not map. Why?

  • A. AT S1E3R ignores the tables and always returns a flat mapping
  • B. The ISB between AT and MRS is missing, so PAR holds a stale value
  • C. TTBR0_EL3 is not used by AT until a TLBI ALLE3
  • D. With SCTLR_EL3.M == 0, stage 1 of the EL3 regime is disabled, so AT reports the flat MMU-off translation. To dry-run tables, probe them in a regime that has its MMU enabled (for example by installing them in the EL1&0 regime and using AT S1E1R from EL3)
Answer

D. AT reports what the regime currently does, and with M == 0 that is the identity mapping with MMU-off attributes. B is a real hazard, but a stale PAR would not systematically show PA == VA for every probe. A is the overgeneralisation: AT uses the tables once stage 1 is enabled.

Read more: Arm ARM (DDI0487), AT S1E3R; “The effects of disabling address translation stages on VMSAv8-64 behavior”

Q5.9.2 A KVM-style hypervisor takes a stage 2 Permission fault with S1PTW == 0 and needs the faulting IPA. Why does it run AT S1E1R on FAR_EL2 instead of reading HPFAR_EL2, and what must the sequence contain?

  • A. HPFAR_EL2 only holds PAs, so AT is needed to get an IPA; no barrier is needed
  • B. For that fault type HPFAR_EL2 may be UNKNOWN. AT S1E1R (with the guest’s EL1 context live) returns the IPA in PAR_EL1, and an ISB is needed between the AT and the MRS PAR_EL1
  • C. HPFAR_EL2 is valid, but AT S12E1R is faster; a DSB is needed before the MRS
  • D. FAR_EL2 already holds the IPA, so the AT is only a consistency check
Answer

B. The architecture does not guarantee HPFAR_EL2 for a stage 2 Permission fault that is not on a stage 1 walk (and some errata widen that). AT S1E1R gives the stage 1 output, which is the IPA. The PAR update is an indirect write, so it needs a context synchronization event before a direct read. DSB (C) is not one. FAR_EL2 holds the VA (D).

Read more: Arm ARM (DDI0487), HPFAR_EL2, PAR_EL1; Linux __translate_far_to_hpfar()

Q5.9.3 AT S1E1R on a kernel VA returns F = 0, ATTR = 0x00, while the kernel intended Normal Write-Back memory. Which conclusion is justified?

  • A. The mapping is fine, because AT succeeded, and ATTR is IMPLEMENTATION DEFINED on success
  • B. MAIR_EL1 is irrelevant, because ATTR reports the raw AttrIndx
  • C. The leaf’s AttrIndx selects a MAIR_EL1 slot that holds Device-nGnRnE (or MAIR_EL1 itself is wrong). Expect alignment faults, DC ZVA faults and failing exclusives later, not at the access
  • D. Stage 2 has forced the memory to Device, so stage 1 cannot be at fault
Answer

C. On success, PAR_EL1.ATTR is the resulting attribute in MAIR encoding, and 0x00 is Device-nGnRnE. Either the AttrIndx is wrong or the MAIR slot is wrong, and the two are told apart by reading MAIR_EL1. D is possible only for S12E1R with stage 2 active. A stage 1-only S1E1R reports stage 1 attributes.

Read more: Arm ARM (DDI0487), PAR_EL1.ATTR; 6.5