target/arm: tlbi_aa64_get_range() rejects valid TG != TCR_ELx.TG and overflows 32-bit shift for large ranges

Host environment

  • Operating system: Ubuntu 24.04.3 LTS
  • Architecture: x86_64
  • QEMU flavor: qemu-system-aarch64
  • QEMU version: 11.1.50 (upstream commit 8967b2fe1b2aaae3c29e207d85fab901d580a2dc); also reproduced on 11.1.2 and confirmed in 8.2.2 (a8dc392d)
  • QEMU command lines:
    # Case 1 (granule mismatch):
    qemu-system-aarch64 -M virt,virtualization=on -cpu max -smp 2 \
      -nodefaults -nographic -serial stdio -semihosting-config enable=on,target=native \
      -device loader,file=guest_tg.elf \
      -device loader,addr=0x40200000,cpu-num=0
    
    # Case 2 (4 GiB 32-bit shift overflow):
    qemu-system-aarch64 -M virt -cpu max -smp 2 \
      -nodefaults -nographic -serial stdio -semihosting-config enable=on,target=native \
      -device loader,file=guest_ovf.elf \
      -device loader,addr=0x40200000,cpu-num=0

Emulated/Virtualized environment

  • Operating system: None (bare-metal test images)
  • Architecture: AArch64 (2 PEs, -cpu max, FEAT_TLBIRANGE; Case 1 uses virtualization=on EL2)

Description of problem

tlbi_aa64_get_range() in target/arm/tcg/tlb-insns.c has two defects when decoding FEAT_TLBIRANGE (TLBI R*E* / TLBI R*E*IS) operands:

  1. Spurious rejection when TG != param.gran (tlb-insns.c:853-861): tlbi_aa64_get_range() queries aa64_va_parameters() on the issuing PE's TCR_ELx and drops the range TLBI whenever the instruction's TG field (Xt[47:46]) differs from param.gran. When an EL2 hypervisor on PE0 (unused TCR_EL1.TG0 = 0b00, 4 KiB) broadcasts TLBI RVAALE1IS with TG = 0b11 (64 KiB) for a 64 KiB-granule EL1 guest on PE1 — or executes a local single-PE EL2 TLBI RVAALE1 with TG = 0b11 — QEMU logs Invalid tlbi page size granule 3 and silently drops the invalidation, leaving PE1 reading stale PA_OLD. Per the Arm Architecture Reference Manual for A-profile architecture (Arm DDI 0487, §C5.5.2 "TLBI RVAALE1IS, TLBI RVAALE1ISNXS" / "TLBI RVAE1IS, TLBI RVAE1ISNXS" and §D8.10, formerly §D5.10.2, "TLB maintenance instructions" — FEAT_TLBIRANGE), the TG field (Xt[47:46]) specifies the translation granule of target TLB entries to match across the shareability domain rather than requiring equality with the issuing PE's current TCR_ELx.TG; only reserved TG == 0b00 (GranInvalid) is invalid.
  2. 32-bit unsigned int shift overflow and shift-count-32 UB (tlb-insns.c:868): Per the Arm Architecture Reference Manual for A-profile architecture (Arm DDI 0487, §C5.5.2 and §D8.10, formerly §D5.10.2, FEAT_TLBIRANGE range encoding), the invalidated range length is (NUM + 1) * 2^(5 * SCALE + 1) * Translation_Granule computed in 64-bit arithmetic. tlbi_aa64_get_range() computes ret.length = (num + 1) << (exponent + page_shift) where num has 32-bit type unsigned int. For a valid 4 GiB range with 4 KiB granule (TG = 4 KiB, SCALE = 3, NUM = 15), 16U << 28 wraps modulo 2^32 to 0, flushing only the base page (0x41000000) and leaving PE1's cached mapping at +4 KiB (0x41001000) stale. For 64 KiB granule with SCALE = 3, exponent + page_shift == 16 + 16 == 32, triggering C shift-count undefined behavior on 32-bit unsigned int.

Both defects affect broadcast cross-PE range TLBIs and local single-PE range TLBIs (confirmed for local EL2 TLBI RVAALE1 via -d guest_errors). Note that tlbi_aa64_get_range() still reads FEAT_LPA2 param.ds (and do_rvae_write() reads tlbbits_for_regime()) from the issuing PE rather than per target PE.

Steps to reproduce

Case 1: Mismatched TG vs issuing PE TCR_EL1.TG0

  1. Boot the 2-PE bare-metal image in EL2 (-M virt,virtualization=on, HCR_EL2.RW = 1, E2H = 0, TGE = 0), enable PE0's EL2 Stage-1 MMU (SCTLR_EL2.{M,C,I} = 1, 4 KiB identity map, Normal WB-RWA Inner Shareable matching PE1 per Arm DDI 0487 §B2.8), leave PE0 TCR_EL1 = 0 (TG0 = 0b00, 4 KiB), map VA_TEST = 0x41000000 to PA_OLD (0x1111222233334444) in 64 KiB Stage-1 tables, and start PE1 via PSCI.
  2. Drop PE1 directly to EL1, enable PE1's 64 KiB Stage-1 MMU (SCTLR_EL1.{M,C,I} = 1, Normal WB-RWA Inner Shareable, TCR_EL1.TG0 = 0b01, TCR_EL1 = 0x0000000200807516), and load VA_TEST (PA_OLD).
  3. On PE0 (EL2), remap VA_TEST to PA_NEW (0xaaaabbbbccccdddd) and issue TLBI RVAALE1IS with TG = 0b11 (64 KiB), SCALE = 0, NUM = 0, BaseADDR = VA_TEST >> 16, followed by DSB ISH.
  4. On PE1, execute DSB ISH; ISB and reload VA_TEST.

Observed output (Case 1, BEFORE):

[TLBI_RANGE_TG] PE0(EL2) TCR_EL1=0x0000000000000000 PE1(EL1,64K) TCR_EL1=0x0000000200807516
[TLBI_RANGE_TG] PE1 load VA_TEST(64K) before=0x1111222233334444 after=0x1111222233334444 expected=0xaaaabbbbccccdddd
VERDICT: BUG_REPRODUCED (EL2 PE0 TG=64K TLBI RVAALE1IS dropped due to PE0 TCR_EL1.TG0=4K!)

Case 2: 32-bit shift overflow on 4 GiB range (TG = 4 KiB, SCALE = 3, NUM = 15)

  1. Boot the 2-PE bare-metal image in EL1 (-M virt) with 4 KiB Stage-1 tables mapping VA_TARGET = 0x41001000 to PA_OLD (0x1111222233334444), start PE1 via PSCI, and have PE1 load VA_TARGET.
  2. On PE0, remap VA_TARGET to PA_NEW (0xaaaabbbbccccdddd) and issue TLBI RVAALE1IS with BaseADDR = 0x41000000 >> 12, TG = 0b01 (4 KiB), SCALE = 3, NUM = 15 (arg = 0x0000778000041000, 4 GiB range), followed by DSB ISH.
  3. On PE1, execute ISB and reload VA_TARGET (0x41001000).

Observed output (Case 2, BEFORE):

[TLBI_RANGE_OVF] TLBI_RVAALE1IS arg=0x0000778000041000 (TG=4K SCALE=3 NUM=0x000000000000000f)
[TLBI_RANGE_OVF] PE1 load VA_TARGET(0x41001000) before=0x1111222233334444 after=0x1111222233334444 expected=0xaaaabbbbccccdddd
VERDICT: BUG_REPRODUCED (4GiB NUM=15 TLBI RVAALE1IS overflowed to 0; PE1 kept stale PA_OLD at +4KB!)

Expected behavior: In both cases, TLBI RVAALE1IS invalidates PE1's cached TLB entry so PE1 reloads PA_NEW (0xaaaabbbbccccdddd).

Standalone bare-metal reproducers for both cases are available to attach.

Additional information

Root cause

In target/arm/tcg/tlb-insns.c at commit 8967b2fe1b2a (lines 853–869 in tlbi_aa64_get_range(); QEMU 8.2.2 target/arm/helper.c:5011-5036):

  • Line 857 checks if (gran != param.gran || gran == GranInvalid), rejecting valid non-zero TG values that differ from the issuing PE's TCR_ELx.TG.
  • Line 868 computes ret.length = (num + 1) << (exponent + page_shift) using 32-bit unsigned int num, overflowing to 0 at 4 GiB (16U << 28) and shifting by 32 (undefined behavior) for 64 KiB SCALE = 3.

Paired controls / Reference

  • Case 1 paired control (3/3): setting PE0 TCR_EL1.TG0 = 0b01 (64 KiB) makes the identical TLBI RVAALE1IS invalidate PE1 (PA_NEW). Running the identical Case 1 candidate guest on Arm Fast Models FVP_Base_RevC-2xAEMvA with -C cache_state_modelled=1 passes 3/3 (VERDICT: SPEC_COMPLIANT), and gem5 (src/arch/arm/tlbi_op.cc:343) matches TG against target TLB entry granules rather than the issuer's TCR_EL1.TG0.
  • Case 2 paired control (3/3): using NUM = 14 (3.75 GiB, 15U << 28 = 0xf0000000, fits in 32 bits) flushes PE1 cleanly (PA_NEW).

Reproducer

Standalone bare-metal reproducer: https://gist.github.com/canxin121/2b9f9b72365ddc21539b137eeba73ed2