diff --git a/runtime/host.h b/runtime/host.h index f5528f18..065256a2 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -167,6 +167,9 @@ void mem_thaw(uint64_t a, uint64_t n); // makes the frozen r void mem_freeze_stats(uint64_t *frozen, uint64_t *freezes, uint64_t *fault_thaws, uint64_t *other_thaws); // regions frozen now; freezes and thaws since the last call void mem_kernel_free(void *p, uint64_t size); // TLR/TLS reuse after thread exit +int mem_guest_readable(uint64_t a, uint64_t n); // [a, a+n) can be read without a fault: inside a loaded + // module or the committed heap answered from the layout, + // anything else by plat_mem_readable (an OS query) // ---- modules (loader.c) typedef struct { diff --git a/runtime/memory.c b/runtime/memory.c index f57117ab..4dcc064c 100644 --- a/runtime/memory.c +++ b/runtime/memory.c @@ -288,6 +288,35 @@ uint32_t mem_set_heap_size(uint64_t size, uint64_t *addr) { return R_OK; } +// The runtime's readers of game objects (menu_cursor.c, mouse_direct.c) check every pointer before they follow it. An +// OS query (plat_mem_readable: VirtualQuery) costs tens of microseconds in this process, and a reader that checks a +// few dozen pointers at a time stalls the game thread for milliseconds. The game's objects live in the committed heap +// and its globals in the modules' images, which are readable for as long as they are mapped: those answer from the +// layout. Any other range asks the OS. The heap only shrinks when the game asks for it (mem_set_heap_size), not while +// it runs. +int mem_guest_readable(uint64_t a, uint64_t n) { + static uint64_t fast, slow; + if (a + n < a) return 0; + uint64_t hs = __atomic_load_n(&heap_size, __ATOMIC_ACQUIRE); + int ok = a >= L.heap.base && a + n <= L.heap.base + hs; + for (int i = 0; !ok && i < ldr_count(); i++) { + const RtLoaded *m = ldr_module(i); + ok = a >= m->base && a + n <= m->base + m->size; + } + if (ok) { + if (__atomic_add_fetch(&fast, 1, __ATOMIC_RELAXED) == 100000) + rt_log("memory: 100000 guest reads checked by the layout, %llu by the OS", + (unsigned long long)__atomic_load_n(&slow, __ATOMIC_RELAXED)); + } else { + ok = plat_mem_readable(a, n); + uint64_t s = __atomic_add_fetch(&slow, 1, __ATOMIC_RELAXED); + if (!(s & (s - 1)) && s >= 1024) // 1024, 2048, 4096...: how often the layout could not answer + rt_log("memory: guest reads checked by the layout %llu, by the OS %llu", + (unsigned long long)__atomic_load_n(&fast, __ATOMIC_RELAXED), (unsigned long long)s); + } + return ok; +} + // ---- module data permissions (0x02, 0x03) and module pages for QueryMemory static int module_index(uint64_t a) { for (int i = 0; i < ldr_count(); i++) { diff --git a/runtime/menu_cursor.c b/runtime/menu_cursor.c index 3120f140..62bdedae 100644 --- a/runtime/menu_cursor.c +++ b/runtime/menu_cursor.c @@ -53,7 +53,7 @@ enum { PANE_PARENT = 0x18, PANE_SIZE = 0x50, PANE_FLAG = 0x58, PANE_ALPHA = 0x59 static uint64_t main_lo, main_hi; static int ok_at(uint64_t p, uint64_t n) { - return p >= 0x10000 && p < (1ull << 47) && (p & 7) == 0 && plat_mem_readable(p, n); + return p >= 0x10000 && p < (1ull << 47) && (p & 7) == 0 && mem_guest_readable(p, n); } static uint64_t rd64(uint64_t a) { uint64_t v; memcpy(&v, (const void *)(uintptr_t)a, 8); return v; } static uint32_t rd32(uint64_t a) { uint32_t v; memcpy(&v, (const void *)(uintptr_t)a, 4); return v; } @@ -157,7 +157,7 @@ static Focus focus_now(void) { static void layout_name(uint64_t scr, char *out, size_t n) { snprintf(out, n, "?"); uint64_t lyt = rd64(scr + SCR_LAYOUT), name = ok_at(lyt, LYT_NAME + 8) ? rd64(lyt + LYT_NAME) : 0; - if (name < 0x10000 || name >= (1ull << 47) || !plat_mem_readable(name, n)) return; + if (name < 0x10000 || name >= (1ull << 47) || !mem_guest_readable(name, n)) return; memcpy(out, (const void *)(uintptr_t)name, n - 1); out[n - 1] = 0; for (char *q = out; *q; q++) if (*q < 0x20 || *q > 0x7e) { *q = 0; break; }