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 usesvirtualization=onEL2)
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:
- Spurious rejection when
TG != param.gran(tlb-insns.c:853-861):tlbi_aa64_get_range()queriesaa64_va_parameters()on the issuing PE'sTCR_ELxand drops the range TLBI whenever the instruction'sTGfield (Xt[47:46]) differs fromparam.gran. When an EL2 hypervisor on PE0 (unusedTCR_EL1.TG0 = 0b00, 4 KiB) broadcastsTLBI RVAALE1ISwithTG = 0b11(64 KiB) for a 64 KiB-granule EL1 guest on PE1 — or executes a local single-PE EL2TLBI RVAALE1withTG = 0b11— QEMU logsInvalid tlbi page size granule 3and silently drops the invalidation, leaving PE1 reading stalePA_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), theTGfield (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 currentTCR_ELx.TG; only reservedTG == 0b00(GranInvalid) is invalid. - 32-bit
unsigned intshift 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_TLBIRANGErange encoding), the invalidated range length is(NUM + 1) * 2^(5 * SCALE + 1) * Translation_Granulecomputed in 64-bit arithmetic.tlbi_aa64_get_range()computesret.length = (num + 1) << (exponent + page_shift)wherenumhas 32-bit typeunsigned int. For a valid 4 GiB range with 4 KiB granule (TG = 4 KiB,SCALE = 3,NUM = 15),16U << 28wraps modulo2^32to0, flushing only the base page (0x41000000) and leaving PE1's cached mapping at+4 KiB(0x41001000) stale. For 64 KiB granule withSCALE = 3,exponent + page_shift == 16 + 16 == 32, triggering C shift-count undefined behavior on 32-bitunsigned 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
- 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 PE0TCR_EL1 = 0(TG0 = 0b00, 4 KiB), mapVA_TEST = 0x41000000toPA_OLD(0x1111222233334444) in 64 KiB Stage-1 tables, and start PE1 via PSCI. - 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 loadVA_TEST(PA_OLD). - On PE0 (EL2), remap
VA_TESTtoPA_NEW(0xaaaabbbbccccdddd) and issueTLBI RVAALE1ISwithTG = 0b11(64 KiB),SCALE = 0,NUM = 0,BaseADDR = VA_TEST >> 16, followed byDSB ISH. - On PE1, execute
DSB ISH; ISBand reloadVA_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)
- Boot the 2-PE bare-metal image in EL1 (
-M virt) with 4 KiB Stage-1 tables mappingVA_TARGET = 0x41001000toPA_OLD(0x1111222233334444), start PE1 via PSCI, and have PE1 loadVA_TARGET. - On PE0, remap
VA_TARGETtoPA_NEW(0xaaaabbbbccccdddd) and issueTLBI RVAALE1ISwithBaseADDR = 0x41000000 >> 12,TG = 0b01(4 KiB),SCALE = 3,NUM = 15(arg = 0x0000778000041000, 4 GiB range), followed byDSB ISH. - On PE1, execute
ISBand reloadVA_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-zeroTGvalues that differ from the issuing PE'sTCR_ELx.TG. - Line 868 computes
ret.length = (num + 1) << (exponent + page_shift)using 32-bitunsigned int num, overflowing to0at 4 GiB (16U << 28) and shifting by32(undefined behavior) for 64 KiBSCALE = 3.
Paired controls / Reference
- Case 1 paired control (3/3): setting PE0
TCR_EL1.TG0 = 0b01(64 KiB) makes the identicalTLBI RVAALE1ISinvalidate PE1 (PA_NEW). Running the identical Case 1 candidate guest on Arm Fast ModelsFVP_Base_RevC-2xAEMvAwith-C cache_state_modelled=1passes 3/3 (VERDICT: SPEC_COMPLIANT), and gem5 (src/arch/arm/tlbi_op.cc:343) matchesTGagainst target TLB entry granules rather than the issuer'sTCR_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