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
