You've hit on a very important point! The "undo" mechanism after speculative execution is where a lot of the complexity (and vulnerabilities) lie. It's not a perfect process, and Meltdown exploits the imperfections.
Here's a breakdown of how the "undo" is supposed to work, and where it falls short:
1. The Goal of Undo
When the CPU detects that speculative execution went down the wrong path (e.g., a permission violation, a branch misprediction), it needs to:
- Rollback the architectural state: This means making sure the registers and memory appear as if the speculative execution never happened. The CPU wants to ensure the program continues as if it had taken the correct path all along.
- Prevent side effects: Minimize any lasting changes to the system's state that could leak information.
2. How Undo is Implemented
CPUs use a few techniques for undoing speculative execution:
- Speculative Store Bypass Buffer (SSBB): This buffer temporarily stores stores that happen during speculative execution. When a misprediction occurs, the SSBB is used to undo those stores.
- Load/Store Queue: CPUs maintain queues of pending loads and stores. When a misprediction happens, these queues are flushed, and any pending operations are discarded.
- Branch Prediction History: The branch predictor is updated to avoid making the same mistake in the future.
3. Where the Undo Fails (and Meltdown Exploits It)
The problem is that undo is not perfect. Specifically:
- Cache Effects are Difficult to Undo: While the CPU can undo register changes and discard store operations, it's very difficult to reliably undo changes to the cache. Flushing the entire cache is too slow and would negate the performance benefits of speculative execution.
- Microarchitectural Side Effects: Other microarchitectural effects (like TLB entries, prefetchers, etc.) are also hard to fully undo.
Meltdown exploits this imperfect undo. The speculative read to kernel memory does cause a fault and the CPU does try to undo the operation. However, the data is briefly loaded into the cache before the undo can fully take effect. This is the window of opportunity for the attacker.
Analogy
Imagine you're building a sandcastle (speculative execution). You start adding towers and walls, but then realize you're building it in the wrong spot. You try to quickly dismantle the towers and walls (undo), but some of the sand has already spread around, changing the shape of the beach (cache effects). An observer can see the changes to the beach and infer something about your original sandcastle design.
Meltdown's Specific Focus
Meltdown specifically targets the fact that the CPU doesn't reliably undo the cache effects of speculative loads. The attacker uses the cache as a covert channel to leak the data that was speculatively read.
Mitigations
Mitigations for Meltdown (like Kernel Page-Table Isolation - KPTI) aim to:
- Separate kernel and user page tables to prevent speculative access to kernel memory.
- Introduce instructions to explicitly flush the cache after certain operations.
In essence: The CPU tries to undo speculative execution, but it can't perfectly erase all the side effects, especially those related to the cache. Meltdown exploits this imperfection to leak sensitive information.