Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

3.4 DMB, DSB, ISB: semantics and where they are needed

Normal memory accesses are weakly ordered in v8-M, as in v8-A. A particular core, such as the in-order Cortex-M33, may appear to keep accesses in order, but the architecture does not guarantee it. The three barriers are:

  • DMB orders explicit memory accesses. Accesses before it are observed before accesses after it. It does not wait for completion, and it does not ensure that side effects of SCS writes have taken effect, or affect instruction fetch.
  • DSB is stronger. No instruction after it in program order executes until it completes. It completes when every explicit memory access before it has completed and all earlier cache and branch-predictor maintenance has completed.
  • ISB is a context synchronization event. Instructions after it are fetched again from cache or memory, so they see the effects of context-changing operations completed before it, such as a CONTROL write or an MPU update that a DSB has completed.

The places where Arm requires or recommends them (see also AN321):

  • After reconfiguring the MPU: DSB so the SCS writes complete, then ISB so the next instructions are fetched and checked under the new regions.
  • After MSR CONTROL (SPSEL, nPRIV, FPCA and so on): ISB, so later instructions use the new stack pointer or privilege.
  • When relocating VTOR: the vector table copy must be complete before the VTOR write, and the VTOR write must complete before an exception can use it. Use copy, DSB, write VTOR, DSB.
  • Before WFI/WFE, when the wake-up condition depends on a memory-mapped write having taken effect (for example clearing a peripheral IRQ flag or arming a timer): DSB.
  • After enabling or disabling an interrupt in the NVIC (ISER/ICER), when the change must apply before the next instruction: DSB then ISB. For example, after a disable, the IRQ must not be taken after the ISB.

Coming from A-profile: These barriers work like AArch64 DMB/DSB/ISB, but v8-M defines only the SY option, with no ISH/OSH/LD/ST variants. Most system control (MPU, VTOR, NVIC, cache maintenance) consists of Device-nGnRnE stores to the SCS rather than MSR to system registers, which is why a DSB to complete the store usually comes before the ISB. CONTROL, BASEPRI and similar registers are still MSR special registers.

Checkpoint

Q3.4.1 After writing new MPU_RBAR/RLAR values and setting MPU_CTRL.ENABLE, which sequence architecturally guarantees that the next instruction is fetched and executed under the new MPU configuration?

  • A. ISB only
  • B. DMB then ISB
  • C. DSB then ISB
  • D. DSB only
Answer

C. The DSB ensures that the stores to the MPU registers have completed, and the ISB then refetches later instructions so they see the new context. DMB only orders memory accesses relative to each other and does not wait for the MPU write to complete. DSB alone does not refetch instructions already fetched under the old configuration.

Read more: Armv8-M ARM (DDI0553), memory barriers (DSB/ISB) and MPU programming requirements

Q3.4.2 In an ISR, code clears a peripheral’s interrupt status bit with a store to Device memory and then returns. Sometimes the ISR is entered again straight away even though the source was cleared. What fixes this architecturally?

  • A. Add a DSB after the store, before exception return, so the clear completes before the interrupt is sampled again
  • B. Add an ISB after the store
  • C. Change the peripheral region to Device-nGnRnE
  • D. Add a DMB before the store
Answer

A. The store may still be in flight, for example in a write buffer, when the exception returns, so the interrupt is still pending or asserted. A DSB waits for the store to complete. On some SoCs a read-back of the peripheral register is also needed, because bus or peripheral latency can delay deassertion of the line. An ISB does not wait for outstanding memory writes. nGnRnE removes early acknowledgement, but the PE still does not wait for the store without a DSB.

Read more: Armv8-M ARM (DDI0553), DSB semantics; Arm AN321, Cortex-M Programming Guide to Memory Barrier Instructions

Q3.4.3 Firmware switches from MSP to PSP with MSR CONTROL, r0 (SPSEL=1). What does Arm require next, before code that depends on the new SP?

  • A. Nothing, because MSR CONTROL takes effect immediately
  • B. DSB
  • C. DMB
  • D. ISB
Answer

D. A change to CONTROL is a context change, and later instructions are only guaranteed to see it after a context synchronization event such as an ISB. CMSIS __set_CONTROL includes one for this reason. A DSB waits for memory accesses but is not a context synchronization event.

Read more: Armv8-M ARM (DDI0553), CONTROL register / MSR and context synchronization