</>EmbeddedInterview

How does the volatile keyword affect ARM load and store instructions, and why is it insufficient for synchronization?

MediumARM Architecture

The volatile keyword in C tells the compiler that a variable may change unexpectedly, so accesses to that variable must not be optimized away.

In ARM processors, volatile affects how the compiler generates LDR (Load Register) and STR (Store Register) instructions. However, it does not guarantee atomicity, memory ordering or multicore synchronization.

1. Without volatile

Consider a memory-mapped hardware status register:

#define STATUS_REG (*(uint32_t *)0x40001000U)

while ((STATUS_REG & 1) == 0)
{
}

The compiler may assume that STATUS_REG does not change inside the loop.

Possible optimized ARM assembly:

LDR R0, =0x40001000
LDR R1, [R0]

LOOP:
    TST R1, #1
    BEQ LOOP

Here, LDR executes only once.

Even if the hardware changes the register value, the CPU continues checking the old value stored in R1. This could result in an infinite loop.

2. With volatile

#define STATUS_REG (*(volatile uint32_t *)0x40001000U)

while ((STATUS_REG & 1) == 0)
{
}

Possible ARM assembly:

LDR R0, =0x40001000

LOOP:
    LDR R1, [R0]
    TST R1, #1
    BEQ LOOP

Now, LDR executes during every iteration.

Without volatileWith volatileLDR R1, [R0]Read only onceTST R1, #1BEQ LOOPLDR R1, [R0]TST R1, #1BEQ LOOPChecks old valueReads every iteration

Assuming the hardware register is mapped with appropriate Device memory attributes, each load reads the memory-mapped peripheral register.

3. Effect of volatile on STR instructions

Without volatile:

uint32_t *reg = (uint32_t *)0x40001000U;

*reg = 10;
*reg = 20;

The compiler may eliminate the first write because it is overwritten immediately.

MOV R1, #20
STR R1, [R0]

With volatile:

volatile uint32_t *reg =
    (volatile uint32_t *)0x40001000U;

*reg = 10;
*reg = 20;

Possible assembly:

MOV R1, #10
STR R1, [R0]

MOV R1, #20
STR R1, [R0]

Both write operations are preserved. This is important for hardware registers where each write may trigger a different hardware operation.

4. Why volatile does not guarantee atomicity

Consider two CPU cores incrementing the same variable.

volatile int count = 0;

count++;

Equivalent ARM assembly:

LDR R1, [R0]      // Read count
ADD R1, R1, #1    // Increment count
STR R1, [R0]      // Write count

Although count++ is a single C statement, it involves three operations.

Race condition between two CPU cores

CPU Core 0 CPU Core 1 Memory
LDR → 0 — 0
— LDR → 0 0
ADD → 1 — 0
— ADD → 1 0
STR → 1 — 1
— STR → 1 1

Expected count: 2

Actual count: 1

One increment is lost. This is called a race condition.

Solution: Atomic operations

ARM64 provides exclusive load and store instructions:

RETRY:
    LDXR W1, [X0]
    ADD  W1, W1, #1
    STXR W2, W1, [X0]
    CBNZ W2, RETRY
  • LDXR: Load exclusive and establish an exclusive reservation.
  • ADD: Increment the value.
  • STXR: Attempt an exclusive store.
  • CBNZ: Retry if the exclusive store fails.

Processors supporting ARM LSE also provide atomic instructions such as LDADD.

These instructions provide atomic read-modify-write operations. Additional ordering semantics may be required depending on the synchronization protocol.

5. Why volatile does not guarantee memory ordering

Consider two CPU cores sharing the following variables:

volatile int data = 0;
volatile int flag = 0;

// CPU Core 0
data = 100;
flag = 1;

// CPU Core 1
if (flag == 1)
    value = data;

The compiler may generate:

// CPU Core 0
STR W0, [X1]      // data = 100
STR W2, [X3]      // flag = 1

// CPU Core 1
LDR W4, [X3]      // Read flag
CBZ W4, END
LDR W5, [X1]      // Read data

END:

Despite using volatile, ARM's weak memory ordering can allow Core 1 to observe flag = 1 before observing data = 100.

This can occur because of CPU and memory-system mechanisms such as:

  • Store buffers.
  • Load/store scheduling.
  • Speculative execution.
  • Memory transaction ordering.

Also, volatile variables do not make this example valid thread synchronization under the standard C memory model.

Solution: Acquire and release semantics

// CPU Core 0
STR  W0, [X1]     // data = 100
STLR W2, [X3]     // Release store flag = 1

// CPU Core 1
LDAR W4, [X3]     // Acquire load flag
CBZ  W4, END
LDR  W5, [X1]     // Read data = 100

END:

STLR and LDAR enforce the required memory ordering when the acquire load observes the release store.

In Linux kernel code:

// CPU Core 0
WRITE_ONCE(data, 100);
smp_store_release(&flag, 1);

// CPU Core 1
if (smp_load_acquire(&flag) == 1)
    value = READ_ONCE(data);

In portable userspace C, use C11 atomics or appropriate synchronization primitives.

6. Does volatile bypass the CPU cache?

No. Volatile is a compiler-level mechanism.

C code with volatileCompilerPreserves required LDR/STR accessesCPU Load/Store UnitMMU / Memory attributesNormal memoryCPU cache / RAMDevice memoryMMIO peripheral

Whether an access is cacheable depends on the memory attributes configured through the MMU or MPU, not on the volatile keyword.

7. Where is volatile used in embedded systems?

Use case Why volatile is used
Memory-mapped registers Hardware can change register values
Hardware status polling Forces repeated register reads
Peripheral control registers Preserves required writes
ISR-shared flags in bare-metal code Prevents compiler from caching the flag in a register
Hardware timer registers Values change independently of software

ISR-shared variables may additionally require atomic operations, interrupt masking or memory barriers depending on their size, access pattern and platform.

In Linux device drivers, prefer readl(), writel() and other appropriate MMIO accessors instead of directly dereferencing volatile pointers.

8. Volatile vs atomic vs memory barrier

Feature Volatile Atomic operations Memory barrier
Prevents elimination of required accesses Yes Yes Not by itself
Atomic read-modify-write No Yes, with appropriate operation No
CPU memory ordering No Depends on ordering semantics Yes
Flushes CPU cache No No No
Multicore synchronization Insufficient Yes, with correct semantics Part of synchronization
Typical ARM instructions LDR, STR LDXR, STXR, LDADD DMB, DSB

9. Important interview points

  • volatile primarily affects compiler optimization.
  • It does not disable CPU out-of-order or speculative execution.
  • It does not bypass or flush the CPU cache.
  • It does not make count++ atomic.
  • It does not automatically generate DMB, DSB, LDAR or STLR instructions.
  • It does not prevent race conditions.

Interview key point: Volatile tells the compiler not to optimize away required memory accesses, but it does not tell the CPU how to order those accesses. For synchronization, use atomic operations, memory barriers, acquire-release semantics or locks as appropriate.