You're asking a critical question about speculative execution: how does the CPU "undo" speculative writes to memory or registers when a branch prediction turns out to be wrong? The answer lies in how modern CPUs isolate speculative state from the architectural state (the visible, committed state of the program). Here's how it works:
1. The ReOrder Buffer (ROB) and Register Renaming
Modern CPUs use a ReOrder Buffer (ROB) and register renaming to manage speculative execution safely.
-
ReOrder Buffer (ROB):
A structure that tracks all in-flight instructions. Instructions execute out of order but commit (retire) in order. Only when an instruction retires does it update the architectural state (registers, memory).
-
Register Renaming:
Instead of writing directly to the actual registers, the CPU uses temporary physical registers. Speculative writes go to these temporary registers. If the speculation is wrong, the CPU simply discards the temporary register values.
Example:
if (x > 5) {
a = 10; // Speculative write
} else {
a = 20; // Non-speculative path
}
- If the CPU predicts the
if branch as taken, it speculatively writes a = 10 to a temporary register.
- If the prediction is wrong, the CPU discards the temporary register and commits
a = 20 instead.
2. Memory Writes During Speculation
Memory writes during speculative execution are buffered and not immediately committed to the cache or main memory. They go through a store buffer.
-
Store Buffer:
A queue that holds speculative memory writes. These writes are not applied to the cache until the instruction retires (i.e., becomes non-speculative).
-
Undoing Speculative Writes:
If the speculation is incorrect, the CPU invalidates the store buffer entries for that path, and the writes never reach the cache or memory.
3. Why Cache Side Channels Still Work
Even though speculative writes are rolled back, speculative reads can still affect the cache. This is the key to Meltdown and Spectre.
-
Speculative Reads:
When the CPU speculatively reads a memory address (e.g., kernel memory), it loads the data into the cache. Even if the read is later discarded (due to a fault), the data remains in the cache temporarily.
-
Why Not Rolled Back?
The cache is a shared microarchitectural resource, and rolling back its state would be too slow and complex. CPUs prioritize performance over undoing cache effects.
4. Summary of Speculative Undo
| Operation | Speculative State | Undo Mechanism |
|---|
| Register Writes | Temporary physical registers | Discard temporary registers |
| Memory Writes | Held in store buffer | Invalidate store buffer entries |
| Memory Reads | Data loaded into cache | Not undone (leaves side effects!) |
5. Why Meltdown Exploits This
Meltdown abuses the fact that speculative reads leave traces in the cache, even if the read was invalid. Here’s the full flow:
- The CPU speculatively reads kernel memory (invalid access).
- The data is loaded into the cache (side effect).
- The CPU detects the invalid access, raises a fault, and rolls back the architectural state.
- But the cache state remains unchanged.
- The attacker uses a cache timing attack (e.g., Flush+Reload) to infer the value of the speculative read.
Key Takeaway
- Architectural state (registers, memory) is rolled back on misprediction.
- Microarchitectural state (cache) is not rolled back, creating a side channel.
This asymmetry is what makes attacks like Meltdown possible. The CPU’s performance optimizations (speculative execution + caching) inadvertently expose secrets through timing side channels.
Let me know if you want a diagram or code example to illustrate this!