From 791daafceb8d954f3e7165ddd84e24c62109a55d Mon Sep 17 00:00:00 2001 From: RichardG867 Date: Sat, 13 Dec 2025 16:45:04 -0300 Subject: [PATCH] Dynarec: Force interpreter on pages that undergo excessive invalidation, improves % on TES Daggerfall and potentially other self-modifying code --- src/86box.c | 3 +++ src/cpu/386_dynarec.c | 30 +++++++++++++++++++++--------- src/include/86box/86box.h | 1 + src/include/86box/mem.h | 6 ++++++ 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/86box.c b/src/86box.c index ba42fb748..67853bf7d 100644 --- a/src/86box.c +++ b/src/86box.c @@ -331,6 +331,7 @@ int scrnsz_y = SCREEN_RES_Y; /* current screen size, Y */ int config_changed; /* config has changed */ int title_update; int framecountx = 0; +int seconds_elapsed = 0; int hard_reset_pending = 0; #if 0 @@ -1974,6 +1975,8 @@ pc_onesec(void) framecount = 0; title_update = 1; + + seconds_elapsed++; } void diff --git a/src/cpu/386_dynarec.c b/src/cpu/386_dynarec.c index 852c25427..865706516 100644 --- a/src/cpu/386_dynarec.c +++ b/src/cpu/386_dynarec.c @@ -404,14 +404,15 @@ exec386_dynarec_dyn(void) # endif int valid_block = 0; +# define INVALIDATION_LIMIT 100 /* amount of invalidations *per second* to kick a page to the interpreter */ + page_t *page = &pages[phys_addr >> 12]; + int can_recompile = (page->inv_count < INVALIDATION_LIMIT); # ifdef USE_NEW_DYNAREC - if (!cpu_state.abrt) + if (!cpu_state.abrt && can_recompile) # else - if (block && !cpu_state.abrt) + if (block && !cpu_state.abrt && can_recompile) # endif { - page_t *page = &pages[phys_addr >> 12]; - /* Block must match current CS, PC, code segment size, and physical address. The physical address check will also catch any page faults at this stage */ @@ -445,16 +446,26 @@ exec386_dynarec_dyn(void) if (valid_block && (block->page_mask & *block->dirty_mask)) { # ifdef USE_NEW_DYNAREC codegen_check_flush(page, page->dirty_mask, phys_addr); - if (block->pc == BLOCK_PC_INVALID) + if (block->pc == BLOCK_PC_INVALID) { valid_block = 0; - else if (block->flags & CODEBLOCK_IN_DIRTY_LIST) + goto invalid_block; + } else if (block->flags & CODEBLOCK_IN_DIRTY_LIST) { block->flags &= ~CODEBLOCK_WAS_RECOMPILED; +invalid_block: # else codegen_check_flush(page, page->dirty_mask[(phys_addr >> 10) & 3], phys_addr); page->dirty_mask[(phys_addr >> 10) & 3] = 0; - if (!block->valid) + if (!block->valid) { valid_block = 0; # endif + if (page->inv_timestamp != seconds_elapsed) { + page->inv_timestamp = seconds_elapsed; + page->inv_count = 1; + } else if (++page->inv_count >= INVALIDATION_LIMIT) { + pclog("Forcing interpreter on page %08X\n", phys_addr & 0xfffff000); + can_recompile = 0; + } + } } if (valid_block && block->page_mask2) { /* We don't want the second page to cause a page @@ -653,7 +664,8 @@ exec386_dynarec_dyn(void) cpu_block_end = 0; x86_was_reset = 0; - codegen_block_init(phys_addr); + if (can_recompile) + codegen_block_init(phys_addr); while (!cpu_block_end) { # ifndef USE_NEW_DYNAREC @@ -731,7 +743,7 @@ exec386_dynarec_dyn(void) cpu_end_block_after_ins = 0; - if ((!cpu_state.abrt || (cpu_state.abrt & ABRT_EXPECTED)) && !new_ne && !x86_was_reset) + if ((!cpu_state.abrt || (cpu_state.abrt & ABRT_EXPECTED)) && !new_ne && !x86_was_reset && can_recompile) codegen_block_end(); if (x86_was_reset) diff --git a/src/include/86box/86box.h b/src/include/86box/86box.h index 67ba5ec1b..a1406d201 100644 --- a/src/include/86box/86box.h +++ b/src/include/86box/86box.h @@ -320,6 +320,7 @@ extern void do_pause(int p); extern double isa_timing; extern int io_delay; extern int framecountx; +extern int seconds_elapsed; extern volatile int cpu_thread_run; extern uint8_t postcard_codes[POSTCARDS_NUM]; diff --git a/src/include/86box/mem.h b/src/include/86box/mem.h index 9051189a6..215dca438 100644 --- a/src/include/86box/mem.h +++ b/src/include/86box/mem.h @@ -224,6 +224,9 @@ typedef struct page_t { uint64_t *byte_dirty_mask; uint64_t *byte_code_present_mask; + + uint32_t inv_count; + uint32_t inv_timestamp; } page_t; extern uint32_t purgable_page_list_head; @@ -250,6 +253,9 @@ typedef struct _page_ { /*Head of codeblock tree associated with this page*/ struct codeblock_t *head; + + uint32_t inv_count; + uint32_t inv_timestamp; } page_t; #endif