The Intel Software Developer's Manual mentions that "instruction fetch and page table accesses can pass locked instructions". What does this mean, and why does it matter?
There's a post that says that many Windows functions begin with a MOV EDI, EDI
instruction, because it is useful for safe code hooking: it can be atomically replaced with a two-byte relative jump. But if fetch accesses to memory can "pass locked instructions", is it possible for the following to happen?
- cpu 0 atomically replaces a
MOV EDI, EDI
instruction with a relative jump - cpu 1 "passes the locked instruction", fetching and executing the stale
MOV EDI, EDI
Would it also be possible for something like this happen?
- cpu 0 atomically replaces a
MOV EDI, EDI
instruction with a relative jump - because instruction fetches can "pass the locked instructions", the replacement of the instruction can be considered non-atomic from the context of instruction fetches, so cpu 1 fetches 1 byte from the stale instruction and 1 byte from the new instruction
From Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 3: "System Programming Guide"
Locked operations are atomic with respect to all other memory operations and all externally visible events. Only instruction fetch and page table accesses can pass locked instructions. Locked instructions can be used to synchronize data written by one processor and read by another processor.
For the P6 family processors, locked operations serialize all outstanding load and store operations (that is, wait for them to complete). This rule is also true for the Pentium 4 and Intel Xeon processors, with one exception. Load operations that reference weakly ordered memory types (such as the WC memory type) may not be serialized.
Link: Why do Windows functions all begin with a pointless MOV EDI, EDI instruction?