feat(libkernel): implement sceKernelSyncOnAddressWait8 and sceKernelSyncOnAddressWait16

Wait8 and Wait16 compare the 1- or 2-byte value at the address with the
comparand and share the waiter queue of the other sizes.
sceKernelSyncOnAddressWake accepts any non-null address: libc's
__std_atomic_notify_one/all pass the atomic's own address, which for 8- and
16-bit atomics need not be 4-byte aligned.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
raghav314
2026-10-06 04:04:46 +05:30
parent bc24783e5b
commit ce6d94793c
3 changed files with 77 additions and 3 deletions
@@ -74,6 +74,22 @@ int APS5_VABI sceKernelSyncOnAddressWait(std::uint32_t* address, std::uint32_t e
return WaitOnAddress(address, expected, timeout, __builtin_return_address(0));
}
int APS5_VABI sceKernelSyncOnAddressWait8(std::uint8_t* address, std::uint8_t expected, const KernelUseconds* timeout, const char* name) {
(void)name;
if (!IsAlignedAddress<std::uint8_t>(reinterpret_cast<std::uintptr_t>(address))) {
APS5_INVALID_ARG_EX;
}
return WaitOnAddress(address, expected, timeout, __builtin_return_address(0));
}
int APS5_VABI sceKernelSyncOnAddressWait16(std::uint16_t* address, std::uint16_t expected, const KernelUseconds* timeout, const char* name) {
(void)name;
if (!IsAlignedAddress<std::uint16_t>(reinterpret_cast<std::uintptr_t>(address))) {
APS5_INVALID_ARG_EX;
}
return WaitOnAddress(address, expected, timeout, __builtin_return_address(0));
}
int APS5_VABI sceKernelSyncOnAddressWait32(std::uint32_t* address, std::uint32_t expected, const KernelUseconds* timeout, const char* name) {
(void)name;
if (!IsAlignedAddress<std::uint32_t>(reinterpret_cast<std::uintptr_t>(address))) {
@@ -92,7 +108,7 @@ int APS5_VABI sceKernelSyncOnAddressWait64(std::uint64_t* address, std::uint64_t
int APS5_VABI sceKernelSyncOnAddressWake(void* address, std::int32_t count) {
const auto key = reinterpret_cast<std::uintptr_t>(address);
if (!IsAlignedAddress<std::uint32_t>(key) || count < 0) {
if (!IsAlignedAddress<std::uint8_t>(key) || count < 0) {
APS5_INVALID_ARG_EX;
}
+59 -1
View File
@@ -12,6 +12,8 @@ extern "C" {
int APS5_VABI sceKernelSyncOnAddressWait(std::uint32_t* address, std::uint32_t expected, const KernelUseconds* timeout, const char* name);
int APS5_VABI sceKernelSyncOnAddressWait32(std::uint32_t* address, std::uint32_t expected, const KernelUseconds* timeout, const char* name);
int APS5_VABI sceKernelSyncOnAddressWait64(std::uint64_t* address, std::uint64_t expected, const KernelUseconds* timeout, const char* name);
int APS5_VABI sceKernelSyncOnAddressWait8(std::uint8_t* address, std::uint8_t expected, const KernelUseconds* timeout, const char* name);
int APS5_VABI sceKernelSyncOnAddressWait16(std::uint16_t* address, std::uint16_t expected, const KernelUseconds* timeout, const char* name);
int APS5_VABI sceKernelSyncOnAddressWake(void* address, std::int32_t count);
}
@@ -198,6 +200,57 @@ static void WakeReleasesWaitersOfEverySize() {
for (const int result : results) Require(result == SCE_OK);
}
static void NarrowWaitsCompareOnlyTheirWidth() {
const KernelUseconds none = 0;
alignas(4) std::uint8_t bytes[4] = {1, 5, 7, 7};
Require(sceKernelSyncOnAddressWait8(&bytes[1], 4, &none, "differs") == SCE_OK);
Require(sceKernelSyncOnAddressWait8(&bytes[1], 5, &none, "timeout") == SCE_KERNEL_ERROR_ETIMEDOUT);
alignas(4) std::uint16_t halves[2] = {1, 0x0705};
Require(sceKernelSyncOnAddressWait16(&halves[1], 0x0005, &none, "high") == SCE_OK);
Require(sceKernelSyncOnAddressWait16(&halves[1], 0x0705, &none, "timeout") == SCE_KERNEL_ERROR_ETIMEDOUT);
}
static void WakeReleasesNarrowWaitersAtTheirOwnAddress() {
alignas(4) std::uint16_t halves[2] = {0, 9};
auto* bytes = reinterpret_cast<std::uint8_t*>(halves);
bytes[1] = 3;
auto* half = &halves[1];
std::atomic<int> started{0};
std::atomic<int> byteFinished{0};
std::atomic<int> halfFinished{0};
int byteResult = -1;
int halfResult = -1;
std::thread byteWaiter([&] {
++started;
byteResult = sceKernelSyncOnAddressWait8(&bytes[1], 3, &FAILSAFE_TIMEOUT, "byte");
++byteFinished;
});
std::thread halfWaiter([&] {
++started;
halfResult = sceKernelSyncOnAddressWait16(half, 9, &FAILSAFE_TIMEOUT, "half");
++halfFinished;
});
AwaitCount(started, 2);
std::this_thread::sleep_for(SETTLE);
Require(sceKernelSyncOnAddressWake(&bytes[0], INT_MAX) == SCE_OK);
Require(sceKernelSyncOnAddressWake(&bytes[3], INT_MAX) == SCE_OK);
std::this_thread::sleep_for(SETTLE);
Require(byteFinished.load() == 0 && halfFinished.load() == 0);
Require(sceKernelSyncOnAddressWake(&bytes[1], 1) == SCE_OK);
AwaitCount(byteFinished, 1);
Require(halfFinished.load() == 0);
Require(sceKernelSyncOnAddressWake(half, 1) == SCE_OK);
AwaitCount(halfFinished, 1);
byteWaiter.join();
halfWaiter.join();
Require(byteResult == SCE_OK && halfResult == SCE_OK);
}
static void RejectsInvalidArguments() {
std::uint32_t words[2] = {1, 1};
auto* misaligned = reinterpret_cast<std::uint32_t*>(reinterpret_cast<unsigned char*>(words) + 1);
@@ -206,7 +259,10 @@ static void RejectsInvalidArguments() {
Require(Rejects([&] { sceKernelSyncOnAddressWait32(nullptr, 1, nullptr, nullptr); }));
Require(Rejects([&] { sceKernelSyncOnAddressWait32(misaligned, 1, nullptr, nullptr); }));
Require(Rejects([&] { sceKernelSyncOnAddressWake(nullptr, 1); }));
Require(Rejects([&] { sceKernelSyncOnAddressWake(misaligned, 1); }));
Require(sceKernelSyncOnAddressWake(misaligned, 1) == SCE_OK);
Require(Rejects([&] { sceKernelSyncOnAddressWait8(nullptr, 1, nullptr, nullptr); }));
Require(Rejects([&] { sceKernelSyncOnAddressWait16(nullptr, 1, nullptr, nullptr); }));
Require(Rejects([&] { sceKernelSyncOnAddressWait16(reinterpret_cast<std::uint16_t*>(reinterpret_cast<unsigned char*>(words) + 1), 1, nullptr, nullptr); }));
Require(Rejects([&] { sceKernelSyncOnAddressWake(words, -1); }));
std::uint64_t values[2] = {1, 1};
@@ -224,5 +280,7 @@ int main() {
SizedWaitsTimeOutWhileTheValueMatches();
Wait64ComparesTheHighHalf();
WakeReleasesWaitersOfEverySize();
NarrowWaitsCompareOnlyTheirWidth();
WakeReleasesNarrowWaitersAtTheirOwnAddress();
RejectsInvalidArguments();
}
+1 -1
View File
@@ -48,7 +48,7 @@ Throughout the project, every function at every stage either **does exactly what
- [sceAgcDcbPrimeUtcl2](../../core/libs/prx/libSceAgc/DcbState/src/Display.cpp) (libSceAgc) - assumed to write the packet `sceAgcAcbPrimeUtcl2` writes: PRIME_UTCL2 has the same 5 dwords on the graphics and compute rings. Not confirmed on a title. Both write a NOP of that size, as there is no TLB to prime
- [sceKernelInternalMemoryGetModuleSegmentInfo](../../core/libs/prx/libkernel/Module/src/Module.cpp) (libkernel) - unknown signature
- [sceKernelSyncOnAddressWait](../../core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp) (libkernel) - the only known caller passes a null timeout and a name string as the fourth argument; the timeout is assumed to point to microseconds like the other kernel waits, and the name is ignored
- [sceKernelSyncOnAddressWait32 / sceKernelSyncOnAddressWait64](../../core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp) (libkernel) - libc's `__std_atomic_wait_direct_32/64` pass a microsecond timeout or null and ignore the result; the by-value comparand, return values and Wait64's 8-byte alignment are assumed
- [sceKernelSyncOnAddressWait8 / sceKernelSyncOnAddressWait16 / sceKernelSyncOnAddressWait32 / sceKernelSyncOnAddressWait64](../../core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp) (libkernel) - libc's `__std_atomic_wait_direct_8/16/32/64` pass a microsecond timeout or null and ignore the result; the by-value comparand, return values and the 2- and 8-byte alignment of Wait16 and Wait64 are assumed
- [sceKernelMapNamedFlexibleMemoryInternal](../../core/libs/prx/libkernel/DirectMemory/Export.cpp) (libkernel) - flag 0x8000 unknown; only the sceKernelMapNamedFlexibleMemory flags are accepted
- [Guest arena](../../core/libs/prx/libc/src/GuestArena.cpp) (libc) - the application map area is assumed to end at 0xFC_0000_0000, as `SCE_KERNEL_APP_MAP_AREA_END_ADDR` does on the PS4. On Windows, host allocations made in the arena before libc loads stay, and a fixed guest mapping over one throws
- [sceLibcInternalBacktraceForGame](../../core/libs/prx/libc/src/HeapDiagnostics.cpp) (libSceLibcInternal, implemented in libc) - unknown signature