I'm developing an emulator for a toy CPU (TR3200 cpu). Actually I have a pure interpreter core, but I'm researching to develop a tracing JIT cpu core. I have doubts about how track if a source machine code that has been jited, is modified by the emulated code( self-modifing code or something like an OS loading a programming to RAM) I'm thinking in using an segment or interval tree for this, but I can't find any information or examples of how this is really handled.
In other words... I know how do JIT (I'm thinking on using asmjit), that I should store jitted code on a map that uses the begin address of the jitted block as index; I have some idea how handle cycle count and devices syncs with jitted code. But I not have a clear idea of how handle when guest program writes on a jitted memory block.
For example: We have an simple OS that has executed a program at addresses 0x100-0x500 and it returned correctly. The JIT cpu core, would (optimistic) generated a native machine code that represents these chunk of code. Now, if the OS loads another program and places it over 0x200-0x300 address block, not should invalidate the old jitted chunk of 0x100-0x500 as it got overwrite. Or a worst case, a program that self-rewrite itself, making jitted chunks invalid. How detect this ?