aarch64-cpu icon indicating copy to clipboard operation
aarch64-cpu copied to clipboard

Add TLBI (TLB Invalidate) instruction wrappers with typed operands

Open MRNIU opened this issue 8 months ago • 3 comments

Summary

Add Rust wrappers for AArch64 TLBI system instructions at EL1, with typed operand encoding that prevents callers from having to manually shift and mask bit fields.

Operand types

  • tlbi::Addr — encodes VA (bits [43:0]) and ASID (bits [63:48]) for VA-based instructions
  • tlbi::Asid — encodes ASID only for aside1 / aside1is

Both types provide new() for structured construction and from_raw() for pre-encoded values.

Instructions covered

Category Instructions
No operand vmalle1, vmalle1is
VA + ASID (Addr) vae1, vale1, vaae1, vaale1 + IS variants
ASID only (Asid) aside1, aside1is

Motivation

The crate already wraps DSB/ISB barrier instructions but has no TLBI support, forcing downstream users to write inline assembly for TLB maintenance. For example, SimpleKernel currently uses raw asm!("tlbi vmalle1", "dsb sy", "isb") in its memory management code because no wrapper is available.

Design

  • Typed operands over raw u64: Inspired by how barrier.rs uses type-level dispatch. Addr and Asid are #[repr(transparent)] newtypes — zero runtime cost, compile-time misuse prevention.
  • Two types, not three: VA+ASID and VA-only share the same Addr type (pass asid = 0 for all-ASID variants like vaae1). The ASID field is RES0/ignored by hardware for these instructions, so a separate type would add API surface without meaningful safety benefit.
  • Macro-generated wrappers: tlbi_no_arg! / tlbi_arg! eliminate repetition across 12 instruction wrappers, following the dmb_dsb! pattern in barrier.rs.
  • Safe functions: Consistent with existing wrappers (nop, wfi, dsb, etc.). The unsafe scope is limited to the asm! call.
  • VA mask (44-bit): Uses 0x0000_0FFF_FFFF_FFFF matching bits [43:0] per Arm ARM, ensuring RES0 bits [47:44] are always zero. This is important for kernel (TTBR1) addresses where upper VA bits are all-ones.
  • Type names: Addr and Asid instead of TlbiAddr/TlbiAsid — avoids redundancy with the tlbi:: module path, following barrier.rs convention (e.g. barrier::SY, not barrier::BarrierSY). Types are not EL-specific, so they can be reused when EL2/EL3 instructions are added later.

Future work (documented in module docs)

  • FEAT_TTL (ARMv8.4): TTL hint in bits [47:44]
  • FEAT_TLBIRANGE (ARMv8.4): Range-based TLBI (RVAE1, RVALE1, etc.)
  • Outer Shareable variants (ARMv8.4): VMALLE1OS, VAE1OS, etc.

Verification

All 12 instructions verified via cross-compilation + disassembly:

Instruction Opcode Operand
tlbi vmalle1 d508871f none (xzr)
tlbi vmalle1is d508831f none (xzr)
tlbi vae1, x0 d5088720 register
tlbi vae1is, x0 d5088320 register
tlbi vale1, x0 d50887a0 register
tlbi vale1is, x0 d50883a0 register
tlbi vaae1, x0 d5088760 register
tlbi vaae1is, x0 d5088360 register
tlbi vaale1, x0 d50887e0 register
tlbi vaale1is, x0 d50883e0 register
tlbi aside1, x0 d5088740 register
tlbi aside1is, x0 d5088340 register

Test plan

  • [x] cargo build --target aarch64-unknown-none-softfloat — compiles
  • [x] cargo build (host target) — compiles
  • [x] cargo fmt --check — passes
  • [x] cargo clippy --target aarch64-unknown-none-softfloat — no warnings
  • [x] Cross-compiled test crate + objdump — all 12 opcodes match ARM ARM encoding

MRNIU avatar Mar 31 '26 02:03 MRNIU

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

🤖 Generated with Claude Code

- If this code review was useful, please react with 👍. Otherwise, react with 👎.

MRNIU avatar Mar 31 '26 16:03 MRNIU

As per https://github.com/rust-embedded/wg/blob/master/CODE_OF_CONDUCT.md#ai-tool-use-policy, please can you confirm that as the author you have reviewed and tested this contribution before submission and that you understand the contents of your submission?

jonathanpallant avatar Apr 01 '26 21:04 jonathanpallant

As per https://github.com/rust-embedded/wg/blob/master/CODE_OF_CONDUCT.md#ai-tool-use-policy, please can you confirm that as the author you have reviewed and tested this contribution before submission and that you understand the contents of your submission?

I confirm that I have reviewed the content. AI participated in the design discussions, but I made the final decisions and manually verified the code.

Verified assembly output on godbolt QEMU tests via SimpleKernel

MRNIU avatar Apr 02 '26 01:04 MRNIU