From b6ab660b0066375a38a5ab17dd2bee26e50f4e58 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Mon, 2 Nov 2026 09:14:10 +0300 Subject: [PATCH 1/4] A zero-byte memory pool initialises as an empty pool in the Vulkan back end vk_pool_init gave a size-0 pool with storage a record: the host import of 0 bytes failed (the driver refuses the memory, VK_ERROR_INVALID_EXTERNAL_HANDLE) and the record went into the pool index as the empty range [mem, mem]. A lookup at that address then found the empty pool before the pool that starts there and reported it unbacked. TotK makes such pools now and then (MacNativeGames d3407e29, 1.2.1). Such a pool now gets no record, as a GPU_NO_ACCESS pool; vk_pool_fini finds none and does nothing. Removal by key already kept a neighbour's range safe at Finalize. --probe-vkexec gains an empty pool part: a zero-byte pool at the start of another, lookups there before and after its Finalize. It failed before ("a lookup at the start of a pool finds the empty pool there"). Co-Authored-By: Claude Opus 5.5 --- runtime/vk_exec_probe.c | 28 ++++++++++++++++++++++++++++ runtime/vk_memory.c | 5 +++++ 2 files changed, 33 insertions(+) diff --git a/runtime/vk_exec_probe.c b/runtime/vk_exec_probe.c index 3663e556..a63fafce 100644 --- a/runtime/vk_exec_probe.c +++ b/runtime/vk_exec_probe.c @@ -845,6 +845,31 @@ static int test_mirror(const GfxBackend *b, uint8_t *guest, int *bytes) { return 0; } +// A pool of zero bytes (TotK makes some now and then; MacNativeGames hit one on 1.2.1) initialises as an empty pool: no +// Vulkan buffer or memory (a buffer or import of 0 bytes is invalid usage) and no pool index entry, so a lookup at its +// address still finds the pool that starts there, before and after the empty pool's Finalize. +static int test_empty_pool(const GfxBackend *b, uint8_t *guest) { + pool_t pool = {.magic = NVN_POOL_MAGIC, .flags = 0x22, .mem = guest + 0xE00000, .size = 0x100000, + .game_mem = guest + 0xE00000}; + pool_t empty = {.magic = NVN_POOL_MAGIC, .flags = 0x22, .mem = guest + 0xE00000, .size = 0, + .game_mem = guest + 0xE00000}; + CHECK(b->pool_init(&pool), "pool_init of a CPU-visible pool for the empty-pool part"); + long errors = vk_validation_errors_total(); + CHECK(b->pool_init(&empty), "pool_init of a zero-byte pool"); + uint64_t a = (uint64_t)(uintptr_t)pool.mem; + VkMemSpan whole, at, after; + CHECK(vk_mem_resolve(a + 16, 16, &whole) && whole.buffer, "the pool beside an empty pool is not resolved"); + CHECK(vk_mem_resolve(a, 0, &at) && at.buffer == whole.buffer && at.offset == whole.offset - 16, + "a lookup at the start of a pool finds the empty pool there"); + CHECK(vk_validation_errors_total() == errors, "the zero-byte pool raised %ld validation errors", + vk_validation_errors_total() - errors); + b->pool_fini(&empty); + CHECK(vk_mem_resolve(a + 16, 16, &after) && after.buffer == whole.buffer, + "the empty pool's Finalize took the pool beside it out of the index"); + b->pool_fini(&pool); + return 0; +} + int probe_vkexec(void) { vk_hdr_off(); // the present checks read 8-bit swapchain pixels layouts_equal = 0; @@ -866,6 +891,7 @@ int probe_vkexec(void) { int nvnx_bytes = 0, mirror_bytes = 0; if (!bad) bad = test_nvnx(b, guest, &nvnx_bytes); if (!bad) bad = test_mirror(b, guest, &mirror_bytes); + if (!bad) bad = test_empty_pool(b, guest); b->finish(); vk_pipeline_shutdown(); // the pipelines and the draw part's stand-ins go before the device vk_exec_draw_shutdown(); @@ -910,5 +936,7 @@ int probe_vkexec(void) { "between submissions mirrored, CPU bytes beside a fill kept, %d bytes compared)\n", mirror_bytes); else printf("probe-vkexec: mirror part skipped (memory mode import)\n"); + printf("probe-vkexec: empty pool part passed (a zero-byte pool at the start of another: no Vulkan objects, lookups " + "there find the other pool before and after its Finalize)\n"); return 0; } diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 39ec43e3..cbcbc165 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -245,6 +245,11 @@ int vk_pool_init(pool_t *p) { // virtual pools that map it. Either gets device-local memory: 1.4.3's CPU never writes its 0x18c pool's storage // (unchanged over 3600 frames of the intro), so the guest storage and the GPU copy need no sync. if ((p->flags & NVN_MEMORY_POOL_FLAGS_GPU_NO_ACCESS) && !(p->flags & NVN_MEMORY_POOL_FLAGS_PHYSICAL)) return 1; + // A pool of zero bytes (TotK makes some now and then) holds no address the GPU can use: it gets no record. A record + // would import 0 bytes (invalid usage; the driver refuses the memory) and put an empty range into the index at the + // pool's address, where a lookup of the pool that starts there found the empty one first. vk_pool_fini finds no + // record for it and does nothing. + if (!p->size) return 1; PoolRec *r = calloc(1, sizeof *r); if (!r) rt_fatal("vk: out of memory for a pool record"); r->key = p; From 1f824a6581d81443b1381fc85ca9b081490d0a36 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Mon, 2 Nov 2026 09:15:54 +0300 Subject: [PATCH 2/4] WaitSynchronization leaves a pending cancel to the next wait that would block Ours took a pending CancelSynchronization first, before the signalled objects and before the timeout-0 return, so a poll or a wait on a signalled object used it up and the blocking wait it was meant for ran on. Horizon checks a signalled object, then timeout 0, then the cancel (mesosphere KSynchronizationObject::Wait; LAS Recomp 1144aea moved the poll the same way): both keep it pending. A cancel that wakes a blocked wait still ends it cancelled: CancelSynchronization now wakes it with R_CANCELLED, which the loop tells apart from a signal's -1. --probe-kernel events part: a cancel sent before a poll and before a wait on a signalled event; each is followed by a blocking wait with a 500 ms timeout. Before: the poll and the signalled wait returned 0xec01 (cancelled) and the blocking waits timed out (0xea01). After: timeout, success, then cancelled both times. Co-Authored-By: Claude Opus 5.5 --- runtime/kernel.c | 21 ++++++++++++++++++--- runtime/kernel_probe.c | 31 +++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/runtime/kernel.c b/runtime/kernel.c index d438b052..9d3e2700 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -1952,8 +1952,13 @@ int kern_svc(Cpu *c, uint32_t imm) { int idx = -1; lock(); g_sync_calls++; + // Horizon's order (mesosphere KSynchronizationObject::Wait): a signalled object, then timeout 0, then a pending + // CancelSynchronization. The cancel is for the next wait that would block: a poll or a wait on a signalled + // object leaves it pending. A cancel that wakes this wait (CancelSynchronization wakes it with R_CANCELLED; a + // signal wakes it with -1 to look again) ends it cancelled, signalled objects or not. + uint32_t woke = 0; for (;;) { - if (t->cancel) { + if (woke == R_CANCELLED) { t->cancel = 0; r = R_CANCELLED; break; @@ -1969,6 +1974,15 @@ int kern_svc(Cpu *c, uint32_t imm) { r = R_OK; break; } + if (ns == 0) { + r = R_TIMEOUT; + break; + } + if (t->cancel) { + t->cancel = 0; + r = R_CANCELLED; + break; + } int64_t left = ns; if (ns > 0) { uint64_t spent = kern_now_ns() - start; @@ -1980,7 +1994,8 @@ int kern_svc(Cpu *c, uint32_t imm) { } t->wobj = n ? hs[0] | (uint64_t)n << 32 : 0; g_sync_blocks++; - if (block(t, W_SYNC, left) == R_TIMEOUT) { + woke = block(t, W_SYNC, left); + if (woke == R_TIMEOUT) { r = R_TIMEOUT; break; } @@ -1995,7 +2010,7 @@ int kern_svc(Cpu *c, uint32_t imm) { RtThread *t = h_get((uint32_t)x[0], O_THREAD); if (t) { t->cancel = 1; - if (t->wkind == W_SYNC) wake(t, (uint32_t)-1); + if (t->wkind == W_SYNC) wake(t, R_CANCELLED); } unlock(); x[0] = t ? R_OK : R_BAD_HANDLE; diff --git a/runtime/kernel_probe.c b/runtime/kernel_probe.c index e226d4a5..2b54d02a 100644 --- a/runtime/kernel_probe.c +++ b/runtime/kernel_probe.c @@ -320,11 +320,17 @@ static void test_arbiter(Cpu *c) { } // ---- events -typedef struct { uint32_t h, r, idx; int64_t timeout; volatile int delay, go; } EvJob; +// pre: a first wait on h0 with timeout t0 (result r0, index idx0) before the wait on h. +typedef struct { uint32_t h, r, idx, h0, r0, idx0; int64_t timeout, t0; volatile int delay, go; int pre; } EvJob; static void event_waiter(Cpu *c) { EvJob *j = arg_p(c, 0); while (j->delay && !j->go) sleep_ms(c, 1); + if (j->pre) { + uint32_t h0[1] = {j->h0}; + j->r0 = sv(c, 0x18, 0, ga(h0), 1, (uint64_t)j->t0); + j->idx0 = (uint32_t)c->x[1]; + } uint32_t hs[1] = {j->h}; j->r = sv(c, 0x18, 0, ga(hs), 1, (uint64_t)j->timeout); j->idx = (uint32_t)c->x[1]; @@ -375,6 +381,26 @@ static void test_events(Cpu *c) { join(c, h); CHECK(g.r == KERN_R_CANCELLED, "wait after a cancel: %#x", g.r); + // A pending cancel is for the next WaitSynchronization that would block (mesosphere KSynchronizationObject::Wait: + // signalled objects first, then timeout 0, then the cancel): a poll times out and a wait on a signalled object + // succeeds, both leaving it pending; the next blocking wait ends cancelled. The blocking waits have a 500 ms + // timeout, so a cancel used up too early shows as a timeout instead of a hang. + CHECK(sv(c, 0x11, hw2, 0, 0, 0) == 0, "SignalEvent of the second event"); + EvJob p = {.h = hr, .timeout = 500000000, .delay = 1, .pre = 1, .h0 = hr, .t0 = 0}; + EvJob q = {.h = hr, .timeout = 500000000, .delay = 1, .pre = 1, .h0 = hr2, .t0 = -1}; + EvJob *pq[2] = {&p, &q}; + for (int i = 0; i < 2; i++) { + h = create(c, a_event_waiter, pq[i], 44, -2); + start(c, h); + CHECK(sv(c, 0x19, h, 0, 0, 0) == 0, "CancelSynchronization before the %s", i ? "signalled wait" : "poll"); + pq[i]->go = 1; + join(c, h); + } + CHECK(p.r0 == KERN_R_TIMEOUT, "poll (timeout 0) with a cancel pending: %#x (want timeout)", p.r0); + CHECK(p.r == KERN_R_CANCELLED, "blocking wait after the poll: %#x (want cancelled)", p.r); + CHECK(q.r0 == 0 && q.idx0 == 0, "wait on a signalled event with a cancel pending: %#x (want success)", q.r0); + CHECK(q.r == KERN_R_CANCELLED, "blocking wait after the signalled wait: %#x (want cancelled)", q.r); + uint32_t sw = kern_event_new(), sr = kern_event_readable(sw); CHECK(wait1(c, sr, 0) == KERN_R_TIMEOUT, "service event before the signal"); kern_event_signal(sw); @@ -382,7 +408,8 @@ static void test_events(Cpu *c) { uint32_t handles[] = {sr, sw, hr2, hw2, hr, hw}; for (unsigned i = 0; i < 6; i++) CHECK(sv(c, 0x16, handles[i], 0, 0, 0) == 0, "CloseHandle %#x", handles[i]); CHECK(sv(c, 0x16, hr, 0, 0, 0) == KERN_R_BAD_HANDLE, "CloseHandle twice"); - printf("events: signal, clear, reset, two handles, cancel while waiting and before, service events, 10 ms " + printf("events: signal, clear, reset, two handles, cancel while waiting and before, a pending cancel kept by a poll " + "and a signalled wait, service events, 10 ms " "timeout after %.2f ms: %s\n", ms, failures == before ? "ok" : "FAIL"); } From 0ab16c21b5c55b2a2a202e91e449c3fd40734444 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Mon, 2 Nov 2026 09:15:55 +0300 Subject: [PATCH 3/4] SignalToAddress modify by waiting count subtracts 2 when it wakes all waiters SignalAndModifyByWaitingCountIfEqual with count <= 0 and at least one waiter stored value - 1; Horizon stores value - 2 (mesosphere KAddressArbiter::SignalAndModifyByWaitingCountIfEqual; yuzu, LAS Recomp and nxnative agree). The word was already updated with compare-and-swap, as LAS Recomp 1144aea made theirs; the count > 0 values were right. --probe-kernel arbiter part expected the old value: it now wants 4 for value 6 with one waiter and count -1, and 4 for two waiters and count 0. Before: 5 for both. After: ok. Co-Authored-By: Claude Opus 5.5 --- runtime/kernel.c | 4 +++- runtime/kernel_probe.c | 13 +++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/runtime/kernel.c b/runtime/kernel.c index 9d3e2700..2e86a005 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -1888,7 +1888,9 @@ int kern_svc(Cpu *c, uint32_t imm) { if (type != 0) { int32_t nv = val + 1; if (type == 2) { - if (cnt <= 0) nv = nw > 0 ? val - 1 : val + 1; + // Waking all (count <= 0) with waiters subtracts 2, not 1 (mesosphere + // KAddressArbiter::SignalAndModifyByWaitingCountIfEqual; yuzu, LAS Recomp and nxnative agree). + if (cnt <= 0) nv = nw > 0 ? val - 2 : val + 1; else nv = nw == 0 ? val + 1 : nw <= cnt ? val - 1 : val; } uint32_t e = (uint32_t)val; diff --git a/runtime/kernel_probe.c b/runtime/kernel_probe.c index 2b54d02a..191a1c28 100644 --- a/runtime/kernel_probe.c +++ b/runtime/kernel_probe.c @@ -305,9 +305,18 @@ static void test_arbiter(Cpu *c) { CHECK(sig(c, 2, 5, 0) == 0 && g_arb == 6, "signal type 2, no waiter, count 0: value %d (want 6)", (int32_t)g_arb); ArbJob jc = {.type = 2, .value = 6, .timeout = -1}; h = arb_spawn(c, &jc); - CHECK(sig(c, 2, 6, -1) == 0 && g_arb == 5, "signal type 2, 1 waiter, count -1: value %d (want 5)", (int32_t)g_arb); + // Waking all (count <= 0) with waiters subtracts 2 (mesosphere KAddressArbiter::SignalAndModifyByWaitingCountIfEqual). + CHECK(sig(c, 2, 6, -1) == 0 && g_arb == 4, "signal type 2, 1 waiter, count -1: value %d (want 4)", (int32_t)g_arb); join(c, h); - CHECK(ja.r == 0 && jb.r == 0 && jc.r == 0, "wait type 2 results %#x %#x %#x", ja.r, jb.r, jc.r); + g_arb = 6; + ArbJob jd = {.type = 2, .value = 6, .timeout = -1}, je = jd; + uint32_t hd = arb_spawn(c, &jd), he = arb_spawn(c, &je); + CHECK(sig(c, 2, 6, 0) == 0 && g_arb == 4, "signal type 2, 2 waiters, count 0: value %d (want 4)", (int32_t)g_arb); + join(c, hd); + join(c, he); + CHECK(ja.r == 0 && jb.r == 0 && jc.r == 0 && jd.r == 0 && je.r == 0, "wait type 2 results %#x %#x %#x %#x %#x", ja.r, + jb.r, jc.r, jd.r, je.r); + g_arb = 5; CHECK(sig(c, 2, 7, 1) == KERN_R_BAD_STATE, "signal type 2 with a different value"); // timeout, invalid type uint64_t t0 = kern_now_ns(); From 53d3f8ea2ea0bc477f6a4b392defe7159e5ea3b0 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Mon, 2 Nov 2026 09:20:32 +0300 Subject: [PATCH 4/4] The 1.2.1 texture loader widens a texture's pool offset unsigned The loader keeps a texture's place in its pool as a 32-bit word (main+0xad4294) and widens it with a sign extend for nvnTextureBuilderSetStorage's offset (main+0xad43f0) and for SetPackagedTextureData's source (main+0xad440c). Past 2 GiB the offset turns negative and the storage lands 4 GiB below the pool (MacNativeGames ca311e2d, seen with a 4K texture pack's heap table in a 3.58 GiB pool). The shipped heap table keeps every pool far below 2 GiB, but TOTK_APP_POOL raised with such a pack's heap table (RomFS mods replace it) can reach it. The new patch set pooloff (TOTK_POOL_OFFSET_PATCH, on by default) widens unsigned: the same value below 2 GiB, right up to 4 GiB. 1.4.3 needs no patch: both of its loaders (main+0x583c34, main+0x27ed214) keep the offset in 64 bits (str x25 / mov x2, x25 / add x1, x25, x8). gen.py accepts both original words, and the lifted C at the sites reads the offset as uint32_t with the set on and as int32_t with it off. Co-Authored-By: Claude Opus 5.5 --- recomp/patches-v121.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/recomp/patches-v121.txt b/recomp/patches-v121.txt index a68eb80a..2a5d1257 100644 --- a/recomp/patches-v121.txt +++ b/recomp/patches-v121.txt @@ -82,6 +82,17 @@ site aspect main 12521cc bd414260 f81f0fea @page:x10 bd403940 f84107ea # ldr s0 site aspect main 19b36f0 1e211800 f81f0fea @page:x10 bd403940 f84107ea # fdiv s0, s0, s1 (viewport) site aspect main 1ecdf68 1e211800 f81f0fea @page:x10 bd403940 f84107ea # fdiv s0, s0, s1 (viewport) +# Texture pool offsets past 2 GiB (MacNativeGames ca311e2d). The texture loader keeps a texture's place in its pool as a +# 32-bit offset (main+0xad4294: sub x25, x9, x10, stored as the word w25) and widens it as a signed number for +# nvnTextureBuilderSetStorage's offset and for SetPackagedTextureData's source (the pool's CPU address + offset). The +# shipped heap table keeps every pool far below 2 GiB; a texture pack's heap table with TOTK_APP_POOL raised can make +# one larger (MacNativeGames saw a 3.58 GiB pool), and every texture past 2 GiB then got storage 4 GiB below its pool. +# The offset is widened unsigned instead, the same value below 2 GiB, right up to 4 GiB. 1.4.3 needs no patch: its +# loaders (main+0x583c34, main+0x27ed214) keep the offset in 64 bits. +set pooloff TOTK_POOL_OFFSET_PATCH 1 +site pooloff main ad43f0 93407f22 2a1903e2 # sxtw x2, w25 -> mov w2, w25 (SetStorage offset) +site pooloff main ad440c 8b39c101 8b394101 # add x1, x8, w25, sxtw -> add x1, x8, w25, uxtw (packaged data source) + # Spin waits, from tools/spin_waits.py --exefs build/extracted/v121/exefs --patches recomp/patches-v121.txt: loops that only # another thread can end; their back-edges pass rt_spin_gate (runtime/kernel.c), TOTK_SPIN=0 keeps the core. spin main ac600 ac620 ac630 # 13 instructions; loads