liveBook · Manning

DMB

As explained in Embedded Software with Rust

DMB is an Arm Data Memory Barrier instruction that enforces ordering of relevant memory accesses without necessarily waiting for them to complete.

DMB (Data Memory Barrier) is an Arm instruction that orders relevant memory accesses across the barrier: earlier accesses must be observed before later accesses. It does not necessarily wait for all operations to complete before the CPU continues executing non-memory instructions. section 5.2.3 section 5.5.2

Program order is not always system-bus visibility order. For example, a write may remain in a buffer while a later read proceeds. A DMB at a hand-off point can enforce the intended order, such as between configuring a peripheral and enabling an interrupt that depends on that configuration. section 5.1.3 section 5.2.3

use core::ptr;
use cortex_m::asm;

unsafe {
    // Example addresses.
    let store_addr: *mut u32 = 0x2000_1000 as *mut u32;
    let load_addr:  *const u32 = 0x4001_0000 as *const u32;

    // Volatile store.
    ptr::write_volatile(store_addr, 0xDEAD_BEEF);

    // DMB: enforce ordering of memory operations.
    // All explicit memory accesses before this point must be observed
    // before any explicit memory access after it.
    asm::dmb();

    // Non-memory computation, memory ordering, not "pausing" the CPU.
    let tmp = 3u32.wrapping_add(4);

    // Will not be reordered before the store.
    let value = ptr::read_volatile(load_addr);
}

This Rust example places cortex_m::asm::dmb() between a volatile store and a volatile load. The addresses are illustrative; the intervening computation emphasizes that DMB orders memory accesses rather than pausing all execution. section 5.2.3

Volatile access controls how the compiler treats an access; it does not itself guarantee hardware-level ordering, completion of buffered writes, or cache maintenance. Use a barrier when system-visible ordering or completion is required. section 5.2.3

Use DSB when preceding memory operations must complete before subsequent instructions execute. DSB is stronger and more costly than DMB. ISB serves a different purpose: it makes subsequent instructions execute under updated instruction-side configuration. section 5.1.3 section 5.2.3

How Embedded Software with Rust covers it

How deep it goes
Explains ordering, buffered-write hazards, and barrier alternatives with a Rust example, while referring detailed synchronization requirements to Arm Application Note AN321.
Examples
Illustrates a volatile store and load separated by DMB, and describes configuring a peripheral before enabling an interrupt.
What you can do after
Apply it via cortex_m::asm::dmb() when memory accesses need ordering.
How it connects
Connects DMB to memory-mapped I/O, volatile access, write buffers, DSB, and ISB.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.