Definition
Instruction-cache coherence is the condition that the instructions a processor fetches are the bytes last written at their addresses, not an older copy held in the instruction cache, a small fast memory of recently fetched instructions, or in the pipeline, which holds instructions fetched ahead of execution. Executable-code publication is the procedure that makes newly written machine code safe to run: it first makes the new code coherent, then directs callers to it with one atomic switch. The Arm architecture, for example, does not require hardware to provide this coherence.
The idea
An updater replaces a function that returns 1 with one that returns 2, writing the new instructions with ordinary store instructions. Stores travel through the data side of the memory system, which has a data cache. The processor fetches instructions through a separate instruction side, which has the instruction cache and the pipeline. Each side can hold its own copy of the same bytes. The new bytes can wait in a write-back data cache, one that writes modified data to memory only later; the instruction cache can keep the old bytes; and the pipeline can hold old instructions already fetched. A call then runs the old code and returns 1, or runs a mixture of old and new bytes that neither version contains.
Picture two clerks, each holding a photocopy of one ledger page. The writing clerk edits a private copy. Before the reading clerk sees the edit, three things must happen: the writer’s copy must be copied back into the ledger, which is called cleaning; the reader’s copy must be thrown away, which is called invalidating; and the reader must drop any lines already read off, which is the pipeline flush. A memory barrier §4.37 only orders the writer’s own edits and tells the reader nothing. The picture fails for processors whose hardware keeps the two sides coherent, and for cores without caches, where only the pipeline can be stale.
How it works
Caches hold copies of memory in fixed-size blocks called cache lines; the Cortex-M7, an Arm microcontroller §6.1 core with optional caches, uses 32-byte lines. It implements Armv7-M, an instruction set architecture §4.38 (the contract between a processor and its software), with that architecture’s extension for digital signal processing. Armv7-M has three barrier instructions.
The data memory barrier (DMB) orders the core’s explicit memory accesses, the loads and stores that instructions perform; the architecture manual states that it does not affect the ordering of any other instructions.
The data synchronization barrier (DSB) holds back every later instruction until earlier memory accesses have completed, including the register writes that request cache maintenance.
The instruction synchronization barrier (ISB) flushes the pipeline, so that later instructions are fetched again from cache or memory.
The cache steps below follow the example for self-modifying code in the Armv7-M Architecture Reference Manual. To publish new code on a core with its caches enabled:
Write the new instructions with ordinary stores into memory that no caller can reach yet.
DSB, so that the stores have completed.
Clean the data cache by address for every line of the new code: write each modified line back to the point of unification (PoU), the point by which this processor’s instruction and data caches are guaranteed to see the same copy of a memory location.
DSB, so that the cleaning has completed.
Invalidate the instruction cache by address for the same lines, discarding old copies, and invalidate the branch predictor, the unit that remembers where earlier branches went.
DSB, so that the invalidations have completed.
ISB, so that the pipeline holds nothing fetched before.
Store the new code’s address into the pointer that callers use, with one atomic store.
Step 8 is immutable publication §9.24: the code never changes after its address becomes visible.
On a core without caches, such as the Cortex-M4 §6.3, steps 3 to 6 drop out, but the DSB of step 2 and the ISB of step 7 remain: the manual warns that an instruction fetched into the pipeline before a change to memory can still execute. A cache that a chip vendor adds outside the core, for example in front of flash memory §6.6, needs the steps in the vendor’s manual. Code in flash memory is written through the flash controller’s erase and program operations, not with ordinary stores.
Worked example
In the Thumb instruction set §4.40, the compact instruction set that Arm microcontroller cores execute, a function that returns 1 is two 16-bit instructions. MOVS r0, #1 places 1 in register r0, where Arm’s standard calling convention returns a function’s result, and BX LR jumps to the return address in the link register §4.45. Their encodings, 0x2001 and 0x4770, are stored in little-endian §4.21 byte order as the bytes 01 20 70 47. The version that returns 2 differs only in the first byte: 02 20 70 47.
Take a Cortex-M7 with both caches enabled, a write-back data cache, and the old function at the hypothetical address 0x20401010 in cacheable RAM, its line already in the instruction cache from a recent call. To expose the stale copies, an updater overwrites the first byte in place with 0x02.
The store lands in the data-cache line that starts at
0x20401000; RAM still holds0x01.A DMB now changes nothing that instruction fetch sees.
The next call fetches
01 20 70 47from the instruction cache and returns 1. Had that line been evicted, the fetch would have read RAM, which still holds0x01, and the call would again return 1.After DSB, clean, DSB, invalidate, DSB and ISB, RAM holds
0x02and the instruction cache holds no copy of the line, so the next call fetches02 20 70 47and returns 2.
A real update writes new code to a fresh place and switches a pointer, as in the steps above.
Maintenance by address acts on whole lines, so the range of new code must be rounded outward. A 40-byte function at 0x20401010 begins at offset 16 (0x10) within its line and ends before offset (0x38), so its last byte is at 0x20401037. Rounding the start down to a multiple of 32 gives 0x20401000; rounding the end 0x20401038 up gives 0x20401040. That range holds lines, at 0x20401000 and 0x20401020, and both need cleaning and invalidating. A loop that counts lines as , where means rounded down, misses the second line, which holds the last bytes of the function.
Stated precisely
Let be the core that will run the new code and the set of addresses the new code occupies. Instruction-cache coherence for holds on from a point in ’s execution onward when every later instruction fetch by from returns the bytes last written there. On one Armv7-M core, steps 2 to 7 above, run by after the last write to , establish it. Step 7 may be any context synchronization event, which the manual defines as an ISB, taking an exception, or returning from an exception. The guarantee assumes that itself wrote with ordinary stores, that nothing writes afterwards, and that no cache outside the core holds a copy of .
Executable-code publication is safe when four conditions hold together.
Coherence: steps 2 to 7 finish before any caller can reach the new code.
Atomic switch: callers reach the code only through one pointer, written by one atomic store and read by one load per call, so each call runs the complete old code or the complete new code.
Permission: the memory holding the new code may be executed.
Lifetime: the old code stays unchanged until a quiescent point §14.34, after which no execution uses the old code or can start using it again.
None of the four says that the new code computes the right result.
Limits and common mistakes
No sequence carries over unchanged between processors. Cores differ in whether they have caches, in their line size, in where the PoU lies, and in whether their hardware keeps instruction fetch coherent; the manual of the exact core and chip decides.
On a multi-core processor, an ISB acts only on the core that executes it. Every core that may run the new code needs its own context synchronization event after the maintenance, and the maintenance must reach every cache from which those cores fetch.
Bytecode §2.4 that an interpreter §2.13 reads is data, never fetched as instructions, so no instruction-cache maintenance applies; publishing it follows the rules for shared data: acquire-release ordering §9.11 on the pointer makes a reader that sees the new address also see the complete bytecode. Machine code that a just-in-time compiler §2.14 produces is executable code and needs the full sequence.
Patching an instruction in place is riskier than switching a pointer. A 32-bit Thumb instruction consists of two 16-bit halves, so an interrupt handler §6.12 or another core that runs the code between the two writes executes half of each version.
A memory protection unit §6.8 (MPU), the hardware that enforces access rules for each memory region, can mark a region execute-never (XN), so that a branch into the region faults. The rule called write xor execute keeps each region writable or executable, never both: the updater writes the code, then makes the region executable before publishing. A change to the MPU settings reaches instruction fetch only after a DSB and an ISB.
On Arm’s microcontroller (M-profile) cores, the lowest bit of a function pointer must be 1, which selects the Thumb instruction set; a branch to an address with that bit clear faults.
Questions answered
Does a memory barrier alone always suffice?
No. A memory barrier orders a processor’s data accesses; it does not make instruction fetch see newly written machine code. On Arm processors, for example, hardware need not keep the instruction cache, a small fast memory of recently fetched instructions, coherent with memory, and the data memory barrier (DMB) affects only explicit memory accesses. New bytes can still wait in a write-back data cache, an old copy can remain in the instruction cache, and old instructions can sit in the pipeline. On an Armv7-M core with caches, such as the Cortex-M7, the architecture manual’s sequence is: a data synchronization barrier (DSB), clean the data cache for the new code’s lines, DSB, invalidate the instruction cache and branch predictor for those lines, DSB, then an instruction synchronization barrier (ISB), which flushes the pipeline. A core without caches, such as the Cortex-M4, still needs the DSB and the ISB.
Only bytecode that an interpreter reads as data leaves instruction fetch out of the picture. Even then, a barrier is only part of safe publication: callers must reach the new code through one atomic pointer switch, and the old code must stay until no execution still uses it.
Related entries: Memory barrier (§4.37), Instruction set architecture (ISA) (§4.38), Dynamic patching and hot update (§14.33), Quiescent point (§14.34), Immutable publication (§9.24), Memory protection unit (MPU) (§6.8). Further reading: [81].