From dbe9057cdf8d2fa628d1a6511febc22a7c4eb03c Mon Sep 17 00:00:00 2001 From: Toni Wilen Date: Sun, 7 Aug 2022 20:46:17 +0300 Subject: [PATCH] Make sure hardware emulated RTG boards don't have barrier at the start of VRAM space to fully support JIT direct. --- debug.cpp | 3 ++- expansion.cpp | 1 + include/memory.h | 7 +++++- memory.cpp | 1 + newcpu.cpp | 9 ++++--- od-win32/mman.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++-- 6 files changed, 75 insertions(+), 7 deletions(-) diff --git a/debug.cpp b/debug.cpp index 6747423f..a0ca1130 100644 --- a/debug.cpp +++ b/debug.cpp @@ -4540,10 +4540,11 @@ static void memory_map_dump_3(UaeMemoryMap *map, int log) size_out /= 1024; size_ext = 'M'; } - _stprintf (txt, _T("%08X %7d%c/%d = %7d%c %s%s %s %s"), (j << 16) | bankoffset, size_out, size_ext, + _stprintf (txt, _T("%08X %7d%c/%d = %7d%c %s%s%c %s %s"), (j << 16) | bankoffset, size_out, size_ext, mirrored, mirrored ? size_out / mirrored : size_out, size_ext, (a1->flags & ABFLAG_CACHE_ENABLE_INS) ? _T("I") : _T("-"), (a1->flags & ABFLAG_CACHE_ENABLE_DATA) ? _T("D") : _T("-"), + a1->baseaddr == NULL ? ' ' : '*', bankmodes[ce_banktype[j]], name); tmp[0] = 0; diff --git a/expansion.cpp b/expansion.cpp index ae76beec..f70bf6c4 100644 --- a/expansion.cpp +++ b/expansion.cpp @@ -403,6 +403,7 @@ static addrbank *expamem_init_last (void) expamem_init_clear2 (); write_log (_T("Memory map after autoconfig:\n")); memory_map_dump (); + mman_set_barriers(false); return NULL; } diff --git a/include/memory.h b/include/memory.h index 4b9ae591..71e1ae63 100644 --- a/include/memory.h +++ b/include/memory.h @@ -109,7 +109,9 @@ enum #define ABFLAG_CACHE_ENABLE_ALL (ABFLAG_CACHE_ENABLE_BOTH | ABFLAG_CACHE_ENABLE_INS_BURST | ABFLAG_CACHE_ENABLE_DATA_BURST) typedef struct { - /* These ones should be self-explanatory... */ + /* These ones should be self-explanatory... + * Do not move. JIT depends on it + */ mem_get_func lget, wget, bget; mem_put_func lput, wput, bput; /* Use xlateaddr to translate an Amiga address to a uae_u8 * that can @@ -145,6 +147,8 @@ typedef struct { uae_u8 *baseaddr_direct_r; uae_u8 *baseaddr_direct_w; uae_u32 startaccessmask; + bool barrier; + uae_u32 protectmode; } addrbank; #define MEMORY_MIN_SUBBANK 1024 @@ -531,6 +535,7 @@ extern bool read_kickstart_version(struct uae_prefs *p); extern void chipmem_setindirect(void); extern void initramboard(addrbank *ab, struct ramboard *rb); extern void loadboardfile(addrbank *ab, struct boardloadfile *lf); +extern void mman_set_barriers(bool); uae_u32 memory_get_long(uaecptr); uae_u32 memory_get_word(uaecptr); diff --git a/memory.cpp b/memory.cpp index 1c9de155..601cb88a 100644 --- a/memory.cpp +++ b/memory.cpp @@ -2162,6 +2162,7 @@ bool mapped_malloc (addrbank *ab) if (md.hasbarrier) { // fill end of ram with ILLEGAL to catch direct PC falling out of RAM. put_long_host(ab->baseaddr + ab->reserved_size, 0x4afc4afc); + ab->barrier = true; } ab->allocated_size = ab->reserved_size; } diff --git a/newcpu.cpp b/newcpu.cpp index 913b9f54..0bbef6e4 100644 --- a/newcpu.cpp +++ b/newcpu.cpp @@ -2163,6 +2163,7 @@ static void prefs_changed_cpu (void) currprefs.int_no_unimplemented = changed_prefs.int_no_unimplemented; currprefs.fpu_no_unimplemented = changed_prefs.fpu_no_unimplemented; currprefs.blitter_cycle_exact = changed_prefs.blitter_cycle_exact; + mman_set_barriers(false); } static int check_prefs_changed_cpu2(void) @@ -6360,8 +6361,9 @@ void m68k_go (int may_quit) set_x_funcs(); if (startup) { - custom_prepare (); - protect_roms (true); + custom_prepare(); + mman_set_barriers(false); + protect_roms(true); } startup = 0; event_wait = true; @@ -6417,7 +6419,8 @@ void m68k_go (int may_quit) #endif run_func(); } - protect_roms (false); + protect_roms(false); + mman_set_barriers(true); in_m68k_go--; } diff --git a/od-win32/mman.cpp b/od-win32/mman.cpp index 545b06ff..6c4922d7 100644 --- a/od-win32/mman.cpp +++ b/od-win32/mman.cpp @@ -968,7 +968,15 @@ void *uae_shmat (addrbank *ab, int shmid, void *shmaddr, int shmflg, struct uae_ return result; } -void unprotect_maprom (void) +// remove possible barrier at the start of this memory region +void uae_mman_unmap(addrbank *ab, struct uae_mman_data *md) +{ + if (canbang && (ab->flags & ABFLAG_ALLOCINDIRECT)) { + virtualfreewithlock(ab->start + natmem_offset, ab->reserved_size, MEM_DECOMMIT); + } +} + +void unprotect_maprom(void) { bool protect = false; for (int i = 0; i < MAX_SHMID; i++) { @@ -989,7 +997,7 @@ void unprotect_maprom (void) } } -void protect_roms (bool protect) +void protect_roms(bool protect) { if (protect) { // protect only if JIT enabled, always allow unprotect @@ -1017,6 +1025,55 @@ void protect_roms (bool protect) } } +// Mark indirect regions (indirect VRAM) as non-accessible when JIT direct is active. +// Beginning of region might have barrier region which is not marked as non-accessible, +// allowing JIT direct to think it is directly accessible VRAM. +void mman_set_barriers(bool disable) +{ + addrbank *abprev = NULL; + for (int i = 0; i < MEMORY_BANKS; i++) { + uaecptr addr = i * 0x10000; + addrbank *ab = &get_mem_bank(addr); + if (ab == abprev) { + continue; + } + int size = 0x10000; + for (int j = i + 1; j < MEMORY_BANKS; j++) { + uaecptr addr2 = j * 0x10000; + addrbank *ab2 = &get_mem_bank(addr2); + if (ab2 != ab) { + break; + } + size += 0x10000; + } + abprev = ab; + if (ab && ab->baseaddr == NULL && (ab->flags & ABFLAG_ALLOCINDIRECT)) { + DWORD old; + if (disable || !currprefs.cachesize || currprefs.comptrustbyte || currprefs.comptrustword || currprefs.comptrustlong) { + if (!ab->protectmode) { + ab->protectmode = PAGE_READWRITE; + } + if (!VirtualProtect(addr + natmem_offset, size, ab->protectmode, &old)) { + size = 0x1000; + VirtualProtect(addr + natmem_offset, size, ab->protectmode, &old); + } + write_log("%08x-%08x = access restored (%08x)\n", addr, size, ab->protectmode); + } else { + if (VirtualProtect(addr + natmem_offset, size, PAGE_NOACCESS, &old)) { + ab->protectmode = old; + write_log("%08x-%08x = set to no access\n", addr, addr + size); + } else { + size = 0x1000; + if (VirtualProtect(addr + natmem_offset, size, PAGE_NOACCESS, &old)) { + ab->protectmode = old; + write_log("%08x-%08x = set to no access\n", addr, addr + size); + } + } + } + } + } +} + int uae_shmdt (const void *shmaddr) { return 0; -- 2.47.3