diff --git a/core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp b/core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp index 733ae7c41..f75b77933 100644 --- a/core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp +++ b/core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp @@ -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(reinterpret_cast(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(reinterpret_cast(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(reinterpret_cast(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(address); - if (!IsAlignedAddress(key) || count < 0) { + if (!IsAlignedAddress(key) || count < 0) { APS5_INVALID_ARG_EX; } diff --git a/core/libs/tests/GuestSyncOnAddress.cpp b/core/libs/tests/GuestSyncOnAddress.cpp index e5954051f..c43e7a66f 100644 --- a/core/libs/tests/GuestSyncOnAddress.cpp +++ b/core/libs/tests/GuestSyncOnAddress.cpp @@ -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(halves); + bytes[1] = 3; + auto* half = &halves[1]; + std::atomic started{0}; + std::atomic byteFinished{0}; + std::atomic 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(reinterpret_cast(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(reinterpret_cast(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(); } diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 5fd46685b..06bf4023a 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -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