From 0672655c9682b424b7cf75345adcfd376f9c1d78 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Fri, 13 Nov 2026 17:09:19 -0500 Subject: [PATCH] Let a destroyed vsync event give its slot when the vsync list is full, instead of ending the process on the 17th. gfx_vsync_bind kept every SystemEventType address the game ever passed to GetDisplayVsyncEvent, never dropped one, and called rt_fatal on the 17th distinct address. A destroyed event also stayed on the list the vsync thread signals 60 times a second. kernel.c now exports kern_system_event_live (mapped guest memory with a nonzero state byte), and with the list full gfx_vsync_bind gives the first slot whose event is no longer live to the new one, which also takes the destroyed event off the vsync thread's list; it stays fatal only when all 16 are live. Each new listing logs "gfx: vsync event ... bound in slot N, M listed", so a long session's log shows how often new addresses come. Not reproduced before the change: the fresh 0.10.0 session's system event count stayed at 9-10 across its area loads, and no log here covers returns to the title. tests/vsync_bind_check.py cuts gfx_vsync_bind from gfx_present.c with a stub liveness check: a 17th event takes the first destroyed slot, an 18th the next, and with 16 live events the 17th still ends with the fatal. system_event_check now also checks kern_system_event_live. The compile check of every runtime file passes. Co-Authored-By: Claude Opus 5.5 --- runtime/gfx_present.c | 18 ++++++- runtime/host.h | 1 + runtime/kernel.c | 4 ++ tests/system_event_check.c | 4 +- tests/vsync_bind_check.py | 102 +++++++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 tests/vsync_bind_check.py diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index 5c592ced..d7eb75f4 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -289,15 +289,29 @@ static void vsync_ensure(void) { plat_rmutex_unlock(&g_start_lock); } +// Lists a vsync event the vsync thread signals. With the list full, an event the game destroyed since +// (kern_system_event_live 0: its state byte is 0 or its memory is gone) gives its slot, which also stops the vsync +// thread signalling it; the list ends the process only when all 16 are live. Each new listing is logged, so a long +// session's log shows how often the game asks for a vsync event at a new address. void gfx_vsync_bind(uint64_t system_event) { plat_mutex_lock(&g_vs_lock); int have = 0; for (int i = 0; i < g_vs_nev; i++) have |= g_vs_ev[i] == system_event; + int slot = g_vs_nev; if (!have) { - if (g_vs_nev == 16) rt_fatal("gfx: more than 16 vsync events"); - g_vs_ev[g_vs_nev++] = system_event; + if (slot == 16) { + slot = 0; + while (slot < 16 && kern_system_event_live(g_vs_ev[slot])) slot++; + if (slot == 16) rt_fatal("gfx: more than 16 live vsync events"); + } else { + g_vs_nev++; + } + g_vs_ev[slot] = system_event; } + int listed = g_vs_nev; plat_mutex_unlock(&g_vs_lock); + if (!have) + rt_log("gfx: vsync event %#llx bound in slot %d, %d listed", (unsigned long long)system_event, slot, listed); vsync_ensure(); } diff --git a/runtime/host.h b/runtime/host.h index ef94b596..7084ef29 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -358,6 +358,7 @@ void kern_event_clear(uint32_t writable); // server-side clear void kern_bind_system_event(Cpu *c, uint64_t ev, int autoclear); // guest thread only void kern_signal_system_event(uint64_t ev); // any thread void kern_clear_system_event(uint64_t ev); // any thread; a bound, live event only +int kern_system_event_live(uint64_t ev); // in mapped guest memory, its state byte nonzero int kern_svc(Cpu *c, uint32_t imm); // thread/sync SVCs; 1 if handled uint64_t kern_now_ns(void); extern uint64_t rt_guest_start_ns; // kern_now_ns when the guest started (boot.c), 0 before diff --git a/runtime/kernel.c b/runtime/kernel.c index 8c17ea89..67182514 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -2259,6 +2259,10 @@ void kern_clear_system_event(uint64_t ev) { if (se_find(ev, NULL) && se_live(ev)) system_event_set(ev, 0); } +// A SystemEventType that still holds an event: mapped guest memory with its state byte nonzero. For services that keep +// their own lists of bound events and reuse the slots of destroyed ones (gfx_present.c gfx_vsync_bind). +int kern_system_event_live(uint64_t ev) { return mem_readable(ev + SE_STATE, 1) && se_live(ev); } + // ---- fibers // The SDK switches fibers by swapping register context, which breaks "RET = C return". Each guest fiber gets a host // fiber (own 256 MiB reserved host stack) and its own Cpu, so a suspended fiber's registers and host frames stay diff --git a/tests/system_event_check.c b/tests/system_event_check.c index b41c627f..732b761b 100644 --- a/tests/system_event_check.c +++ b/tests/system_event_check.c @@ -5,7 +5,7 @@ // number went to an unrelated event or the state byte reads nonzero again; a host thread signalling // while a guest thread destroys and binds the event again never ends the process; with the table full, // a new bind takes a destroyed guest-memory entry and keeps the entries in host memory, which a host -// thread then still signals +// thread then still signals; kern_system_event_live tells live guest events from destroyed ones // full every entry live: the next bind ends with "table full" (the script checks the exit code and text) #include "kernel.c" @@ -111,7 +111,9 @@ static void stale_and_reuse(void) { CHECK(signaled(wh) == 1, "a live bound event is signalled"); kern_clear_system_event(ev_of(ev)); CHECK(signaled(wh) == 0, "a live bound event is cleared"); + CHECK(kern_system_event_live(ev_of(ev)), "kern_system_event_live: a bound event in guest memory is live"); destroy(ev); + CHECK(!kern_system_event_live(ev_of(ev)), "kern_system_event_live: a destroyed event is not"); kern_signal_system_event(ev_of(ev)); // state byte 0: skipped before the kernel lock // The byte reads nonzero again with the handles closed (the memory reused, or the SDK made the event again // without the binder): the signal must neither end the process nor reach anything. diff --git a/tests/vsync_bind_check.py b/tests/vsync_bind_check.py new file mode 100644 index 00000000..51a154cd --- /dev/null +++ b/tests/vsync_bind_check.py @@ -0,0 +1,102 @@ +"""The vsync event list reuses the slots of destroyed events (runtime/gfx_present.c gfx_vsync_bind): builds +gfx_vsync_bind, cut from gfx_present.c by its text, with stand-ins for the lock, the log, the vsync thread start and +kern_system_event_live, and checks that + + - binding an address already listed changes nothing; + - with 16 live events listed, a 17th takes the slot of the first destroyed one and keeps the others; + - with all 16 live, a 17th still ends the process with the "more than 16 live vsync events" fatal. + +No game files. Usage: python tests/vsync_bind_check.py [--cc ] +""" +import argparse +import os +import subprocess +import sys +import tempfile +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +MSYS_BIN = Path(r'C:\msys64\ucrt64\bin') + +HARNESS = r''' +#include +#include +#include +#include +typedef int PlatMutex; +#define PLAT_MUTEX_INIT 0 +static void plat_mutex_lock(PlatMutex *m) { (void)m; } +static void plat_mutex_unlock(PlatMutex *m) { (void)m; } +static void rt_log(const char *fmt, ...) { (void)fmt; } +static void rt_fatal(const char *fmt, ...) { printf("FATAL: %%s\n", fmt); fflush(stdout); exit(3); } +static int dead[64]; // events the game destroyed, by address (1..63) +static int kern_system_event_live(uint64_t ev) { return ev < 64 && !dead[ev]; } +static int ensured; +static void vsync_ensure(void) { ensured++; } +static PlatMutex g_vs_lock = PLAT_MUTEX_INIT; +static uint64_t g_vs_ev[16]; +static int g_vs_nev; +%s +static int fails; +#define CHECK(cond, ...) do { if (!(cond)) { printf("FAIL: " __VA_ARGS__); putchar('\n'); fails++; } } while (0) +static int listed(uint64_t ev) { + for (int i = 0; i < g_vs_nev; i++) + if (g_vs_ev[i] == ev) return 1; + return 0; +} +int main(int argc, char **argv) { + for (uint64_t ev = 1; ev <= 16; ev++) gfx_vsync_bind(ev); + gfx_vsync_bind(5); + CHECK(g_vs_nev == 16, "16 events listed, an address bound again is not listed twice (%%d)", g_vs_nev); + CHECK(ensured == 17, "every bind makes sure the vsync thread runs (%%d)", ensured); + if (argc > 1 && !strcmp(argv[1], "full")) { + gfx_vsync_bind(17); // every listed event is live + printf("no fatal\n"); + return 0; + } + dead[7] = dead[9] = 1; + gfx_vsync_bind(17); + CHECK(g_vs_nev == 16 && listed(17) && !listed(7) && listed(9), "a 17th takes the first destroyed slot only"); + for (uint64_t ev = 1; ev <= 16; ev++) + if (ev != 7) CHECK(listed(ev), "event %%llu stays listed", (unsigned long long)ev); + gfx_vsync_bind(18); + CHECK(listed(18) && !listed(9), "an 18th takes the next destroyed slot"); + printf(fails ? "vsync bind: %%d failures\n" : "vsync bind: ok\n", fails); + return fails != 0; +} +''' + + +def cut(text, start, end_marker): + a = text.index(start) + b = text.index(end_marker, a) + return text[a:b] + + +def main(): + ap = argparse.ArgumentParser(description=__doc__) + ap.add_argument('--cc', help='the C compiler (default: MSYS2 UCRT64 clang on Windows, clang elsewhere)') + args = ap.parse_args() + win = os.name == 'nt' + cc = args.cc or (str(MSYS_BIN / 'clang.exe') if win and MSYS_BIN.exists() else 'clang') + env = dict(os.environ) + if win and MSYS_BIN.exists(): + env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '') + src = (ROOT / 'runtime' / 'gfx_present.c').read_text(encoding='utf-8') + body = cut(src, 'void gfx_vsync_bind(uint64_t system_event) {', '\nvoid gfx_vsync_bind_counter(') + with tempfile.TemporaryDirectory(prefix='nx2navi-vsync-bind-') as tmp: + c = Path(tmp) / 'check.c' + c.write_text(HARNESS % body, encoding='utf-8') + exe = Path(tmp) / ('check.exe' if win else 'check') + subprocess.check_call([cc, '-O2', '-Wall', '-Wno-unused-function', str(c), '-o', str(exe)], env=env) + rc = subprocess.call([str(exe)]) + p = subprocess.run([str(exe), 'full'], capture_output=True, text=True) + if p.returncode != 3 or 'more than 16 live vsync events' not in p.stdout: + print(f'FAIL: a 17th live vsync event must end with the fatal (exit {p.returncode}: {p.stdout.strip()})') + return 1 + print('vsync bind full: ok (fatal with 16 live events)') + return rc + + +if __name__ == '__main__': + sys.exit(main())