From 0ab16c21b5c55b2a2a202e91e449c3fd40734444 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Mon, 2 Nov 2026 09:15:55 +0300 Subject: [PATCH] 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();