unicorn-engine / unicorn

Unicorn CPU emulator framework (ARM, AArch64, M68K, Mips, Sparc, PowerPC, RiscV, S390x, TriCore, X86)
http://www.unicorn-engine.org
GNU General Public License v2.0
7.34k stars 1.31k forks source link

Avoid notdirty_write #1839

Open tunz opened 1 year ago

tunz commented 1 year ago

Attempt to avoid notdirty_write as much as possible. Please refer to #1838 for the context.

If there is no TB in the page, we can skip the slow path, so mark the TLB as dirty in such case. And PageDesc is allocated only when TB is generated, so we can apply the same logic for the case where PageDesc is not found.

If Unicorn generates a TB, we have to take the slow path for the page. In tb_page_add, if it's the first tb generated, tlb_protect_code will be called, and it will call cpu_physical_memory_test_and_clear_dirty. In this function, we reset the dirty flag so that Unicorn calls notdirty_write when memory is overwritten in the page.

Tested with tests/regress/x86_self_modifying.py.