0
0
Cnc-programmingHow-ToIntermediate · 4 min read

How to Flush Cache in ARM: Simple Steps and Example

To flush cache in ARM, use the DC CIVAC instruction which cleans and invalidates the data cache for a specified memory range. This ensures data consistency between cache and memory by writing back modified cache lines and invalidating them.
📐

Syntax

The main instruction to flush cache in ARM is DC CIVAC, <address>. Here:

  • DC stands for Data Cache operation.
  • CIVAC means Clean and Invalidate by Virtual Address to Point of Coherency.
  • <address> is the memory address whose cache line you want to flush.

This instruction writes back any modified data in the cache line to memory and invalidates that cache line to ensure fresh data on next access.

armasm
DC CIVAC, <address>
💻

Example

This example shows how to flush the cache line for a specific address in ARM assembly:

armasm
    LDR     X0, =buffer_address  // Load address to X0
    DC      CIVAC, X0             // Clean and invalidate cache line at X0
    DSB     ISH                  // Ensure completion of cache maintenance
    ISB                          // Synchronize context

buffer_address: .quad 0x40000000
⚠️

Common Pitfalls

Common mistakes when flushing cache in ARM include:

  • Not using DSB (Data Synchronization Barrier) after cache operations, which can cause incomplete flushing.
  • Skipping ISB (Instruction Synchronization Barrier), leading to stale instruction fetches.
  • Flushing cache without aligning the address to cache line size, which may leave some cache lines uncleared.
  • Using legacy or incorrect cache maintenance instructions that do not work on newer ARM architectures.
armasm
    // Wrong: Missing barriers
    DC CIVAC, X0

    // Right: With barriers
    DC CIVAC, X0
    DSB ISH
    ISB
📊

Quick Reference

Summary of key cache flush instructions in ARM:

InstructionDescription
DC CIVAC,
Clean and invalidate data cache line by virtual address
DSB ISHData Synchronization Barrier to ensure cache operation completion
ISBInstruction Synchronization Barrier to synchronize instruction stream
DC IVAC,
Invalidate data cache line by virtual address (no clean)
DC CVAC,
Clean data cache line by virtual address (write back only)

Key Takeaways

Use the DC CIVAC instruction to clean and invalidate cache lines by address.
Always follow cache maintenance instructions with DSB and ISB barriers.
Align addresses to cache line size before flushing to ensure full coverage.
Avoid legacy cache instructions; use ARMv8-A recommended instructions.
Proper cache flushing ensures data consistency between CPU cache and memory.