Assume this scenario:
We are going to patch a piece of instruction in executable memory while it's running in another thread, here a sample code in C:
Instruction* mem; // pointer to executable memory.
int offset;
void patch(Instruction new_insn){
mem[offset] = new_insn;
}
Here is what could happen:
1- Thread 1 loads a cache line sized
section of memory into Instruction Cache
to execute.
2- Thread 2 will patch one instruction in the same address space using the code above, the new instruction first get stored in Data Cache
before being written to memory.
3- Now the instruction in Instruction Cache
is invalid as it's outdated. Does the data cache communicate with the instruction cache to invalidate its data so it refetches the data? does the data get copied from one cache to the other, OR the instruction cache need to refetch from memory?