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 diff --git a/runtime/kernel.c b/runtime/kernel.c index d438b052..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; @@ -1952,8 +1954,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 +1976,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 +1996,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 +2012,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..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(); @@ -320,11 +329,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 +390,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 +417,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"); } 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;