feat(libkernel): wake each listed address in _umtx_op UMTX_OP_NWAKE_PRIVATE

This commit is contained in:
Semir Kabir 2026-10-11 17:48:06 +00:00
parent a11c8f7f70
commit e92547a7a1
3 changed files with 80 additions and 1 deletions

View File

@ -27,6 +27,7 @@ constexpr int UMTX_OP_WAKE = 3;
constexpr int UMTX_OP_WAIT_UINT = 11;
constexpr int UMTX_OP_WAIT_UINT_PRIVATE = 15;
constexpr int UMTX_OP_WAKE_PRIVATE = 16;
constexpr int UMTX_OP_NWAKE_PRIVATE = 21;
constexpr int UMTX_OP_MAX = 27;
constexpr std::uint32_t UMTX_ABSTIME = 1;
constexpr int GUEST_CLOCK_REALTIME = 0;
@ -183,6 +184,21 @@ int UmtxWake(void* object, std::uint64_t count, bool isPrivate) {
return 0;
}
int UmtxNwakePrivate(void* object, std::uint64_t value) {
const auto count = static_cast<std::int32_t>(value);
if (count <= 0) {
return 0;
}
if (object == nullptr) {
return GUEST_EFAULT;
}
const auto* addresses = static_cast<void* const*>(object);
for (std::int32_t index = 0; index < count; ++index) {
WakeAddress(reinterpret_cast<std::uintptr_t>(addresses[index]), std::numeric_limits<std::int32_t>::max(), true);
}
return 0;
}
} // namespace
extern "C" {
@ -252,6 +268,9 @@ int APS5_VABI _umtx_op_nid_postfix(void* object, int operation, std::uint64_t va
case UMTX_OP_WAKE_PRIVATE:
error = UmtxWake(object, value, operation == UMTX_OP_WAKE_PRIVATE);
break;
case UMTX_OP_NWAKE_PRIVATE:
error = UmtxNwakePrivate(object, value);
break;
default:
if (operation < 0 || operation >= UMTX_OP_MAX) {
error = GUEST_EINVAL;

View File

@ -20,6 +20,7 @@ static constexpr int UMTX_OP_MUTEX_LOCK = 5;
static constexpr int UMTX_OP_WAIT_UINT = 11;
static constexpr int UMTX_OP_WAIT_UINT_PRIVATE = 15;
static constexpr int UMTX_OP_WAKE_PRIVATE = 16;
static constexpr int UMTX_OP_NWAKE_PRIVATE = 21;
static constexpr int UMTX_OP_SEM2_WAIT = 23;
static constexpr int UMTX_OP_MAX = 27;
static constexpr std::uint32_t UMTX_ABSTIME = 1;
@ -211,6 +212,63 @@ static void WakeWithoutWaitersSucceeds() {
Require(Umtx(&word, UMTX_OP_WAKE_PRIVATE, INT_MAX) == 0);
}
static void NwakeWakesEveryWaiterOnEachListedAddress() {
std::uint32_t first = 1;
std::uint64_t second = 1;
std::uint32_t unlisted = 1;
std::atomic<int> started{0};
std::atomic<int> finished{0};
std::atomic<int> unlistedFinished{0};
std::vector<int> results(4, -2);
int unlistedResult = -2;
std::vector<std::thread> waiters;
for (int i = 0; i < 3; ++i) {
waiters.emplace_back([&, i] {
KernelTimespec failsafe = FailsafeTimeout();
++started;
results[i] = Umtx(&first, UMTX_OP_WAIT_UINT_PRIVATE, 1, nullptr, &failsafe);
++finished;
});
}
waiters.emplace_back([&] {
KernelTimespec failsafe = FailsafeTimeout();
++started;
results[3] = Umtx(&second, UMTX_OP_WAIT, 1, nullptr, &failsafe);
++finished;
});
std::thread unlistedWaiter([&] {
KernelTimespec failsafe = FailsafeTimeout();
++started;
unlistedResult = Umtx(&unlisted, UMTX_OP_WAIT_UINT, 1, nullptr, &failsafe);
++unlistedFinished;
});
AwaitCount(started, 5);
std::this_thread::sleep_for(SETTLE);
void* addresses[] = {&first, nullptr, &second};
Require(Umtx(addresses, UMTX_OP_NWAKE_PRIVATE, 3) == 0);
AwaitCount(finished, 4);
std::this_thread::sleep_for(SETTLE);
Require(unlistedFinished.load() == 0);
Require(Umtx(&unlisted, UMTX_OP_WAKE_PRIVATE, 1) == 0);
for (auto& waiter : waiters) waiter.join();
unlistedWaiter.join();
for (const int result : results) Require(result == 0);
Require(unlistedResult == 0);
}
static void NwakeReadsOnlyTheCountedEntries() {
Require(Umtx(nullptr, UMTX_OP_NWAKE_PRIVATE, 0) == 0);
Require(Umtx(nullptr, UMTX_OP_NWAKE_PRIVATE, 0xffffffff00000000ULL) == 0);
Require(Umtx(nullptr, UMTX_OP_NWAKE_PRIVATE, 0xffffffffULL) == 0);
Require(FailsWith(Umtx(nullptr, UMTX_OP_NWAKE_PRIVATE, 1), GUEST_EFAULT));
std::uint32_t word = 0;
void* addresses[] = {&word};
Require(Umtx(addresses, UMTX_OP_NWAKE_PRIVATE, 1) == 0);
}
static void RejectsBadObjectsAndOperations() {
Require(FailsWith(Umtx(nullptr, UMTX_OP_WAIT, 0), GUEST_EFAULT));
Require(FailsWith(Umtx(nullptr, UMTX_OP_WAIT_UINT, 0), GUEST_EFAULT));
@ -244,5 +302,7 @@ int main() {
WakeReleasesAWaiterWithoutAValueChange();
WakeHonoursTheCountAndTheAddress();
WakeWithoutWaitersSucceeds();
NwakeWakesEveryWaiterOnEachListedAddress();
NwakeReadsOnlyTheCountedEntries();
RejectsBadObjectsAndOperations();
}

View File

@ -169,7 +169,7 @@ Throughout the project, every function at every stage either **does exactly what
- [sceKernelGetModuleInfo](../../core/libs/prx/libkernel/Module/src/ModuleInfo.cpp) (libkernel) - the 0x160-byte layout (`st_size`, `name[256]`, 4 segments, `segment_count`, `fingerprint[20]`) follows [fpPS4](https://github.com/red-prig/fpPS4/blob/04cefd43e6fddd1ab033e7980cd356d14c964905/sys/sys_types.pas#L319-L337) and [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/0fe263a4760dfbfa973366890061749b4af0de97/src/core/module.h#L15-L31). Only that `st_size` is accepted, as in shadPS4; other sizes return `EINVAL`, a null pointer `EFAULT` and a handle `sceKernelGetModuleList` does not list `ESRCH`. Name and segments are the ones `sceKernelGetModuleInfoFromAddr` reports, on Windows from the same PE sections, so `prot` uses the read/write/execute bits 1/2/4, while fpPS4 and shadPS4 store the ELF `p_flags`. The fingerprint is left zero, as the host image does not carry it
- [sceKernelReleaseFlexibleMemory](../../core/libs/prx/libkernel/DirectMemory/Export.cpp) (libkernel) - unmaps the range like sceKernelMunmap, as fpPS4 and prosper do; whether the console keeps the virtual range reserved after releasing its pages is unverified
- [Memory protection bits 0x100 and 0x200](../../core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp) (libkernel) - taken as audio coprocessor (ACP) read and write, as in the [KytyPS5](https://github.com/KytyPS5/KytyPS5) and [etaHEN](https://github.com/etaHEN/etaHEN) headers; the coprocessor work runs on the host CPU, so they grant host read and read/write access like the GPU bits
- [_umtx_op](../../core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp) (libkernel) - `UMTX_OP_WAIT`, `UMTX_OP_WAKE`, `UMTX_OP_WAIT_UINT`, `UMTX_OP_WAIT_UINT_PRIVATE` and `UMTX_OP_WAKE_PRIVATE` follow FreeBSD 11's [kern_umtx.c](https://github.com/freebsd/freebsd-src/blob/stable/11/sys/kern/kern_umtx.c): a timeout whose size argument is at most 16 is a relative `timespec`, a larger one an `_umtx_time`, and a wake of 0 or fewer wakes one waiter. Waiters share the queue of `sceKernelSyncOnAddressWait`. Operation 0 fails with `EOPNOTSUPP` and operations above 26 with `EINVAL`, as in FreeBSD 11; `UMTX_OP_WAIT_UINT` with a value that does not fit in 32 bits returns 0 at once, as FreeBSD's zero-extended compare does. Operation 1 throws, since PPSA12544's call_once uses it in a way FreeBSD rejects; the other operations throw, and so does a misaligned object. PPSA12544's `libc.prx` passes `UMTX_OP_WAIT` and `UMTX_OP_WAIT_UINT` a pointer to a 4-byte microsecond timeout with size argument 0, which FreeBSD reads as a `timespec`, and waits for a running call_once with operation 1; its `PS5Util.prx` passes a `timespec`. Whether the console reads a microsecond timeout there, and what operation 1 does on it, is unverified. A libc call cannot be told apart from a `timespec` call by the size argument alone, so a timeout with size argument 0 is read as a `timespec` and only throws when it is not a valid one (negative seconds, nanoseconds outside [0, 1e9)), which is where FreeBSD would return `EINVAL`; a microsecond value that happens to read as a valid `timespec` is waited on as one
- [_umtx_op](../../core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp) (libkernel) - `UMTX_OP_WAIT`, `UMTX_OP_WAKE`, `UMTX_OP_WAIT_UINT`, `UMTX_OP_WAIT_UINT_PRIVATE`, `UMTX_OP_WAKE_PRIVATE` and `UMTX_OP_NWAKE_PRIVATE` follow FreeBSD 11's [kern_umtx.c](https://github.com/freebsd/freebsd-src/blob/stable/11/sys/kern/kern_umtx.c): a timeout whose size argument is at most 16 is a relative `timespec`, a larger one an `_umtx_time`, and a wake of 0 or fewer wakes one waiter. `UMTX_OP_NWAKE_PRIVATE` reads the value as an `int` count of pointers at the object and wakes every waiter on each one; a count of 0 or fewer reads nothing, and a null list fails with `EFAULT`, where FreeBSD's `copyin` would also fail for any other unreadable list. Waiters share the queue of `sceKernelSyncOnAddressWait`. Operation 0 fails with `EOPNOTSUPP` and operations above 26 with `EINVAL`, as in FreeBSD 11; `UMTX_OP_WAIT_UINT` with a value that does not fit in 32 bits returns 0 at once, as FreeBSD's zero-extended compare does. Operation 1 throws, since PPSA12544's call_once uses it in a way FreeBSD rejects; the other operations throw, and so does a misaligned object. PPSA12544's `libc.prx` passes `UMTX_OP_WAIT` and `UMTX_OP_WAIT_UINT` a pointer to a 4-byte microsecond timeout with size argument 0, which FreeBSD reads as a `timespec`, and waits for a running call_once with operation 1; its `PS5Util.prx` passes a `timespec`. Whether the console reads a microsecond timeout there, and what operation 1 does on it, is unverified. A libc call cannot be told apart from a `timespec` call by the size argument alone, so a timeout with size argument 0 is read as a `timespec` and only throws when it is not a valid one (negative seconds, nanoseconds outside [0, 1e9)), which is where FreeBSD would return `EINVAL`; a microsecond value that happens to read as a valid `timespec` is waited on as one
- [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
- [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
- [scePthreadRwlockDestroy, pthread_rwlock_destroy](../../core/libs/prx/libkernel/Pthread/src/Rwlock.cpp) (libkernel) - a rwlock held for reading or writing returns `EBUSY` and keeps its handle, as [KytyPS5](https://github.com/KytyPS5/KytyPS5) does; FreeBSD's [libthr](https://github.com/freebsd/freebsd-src/blob/main/lib/libthr/thread/thr_rwlock.c) and shadPS4 free it without checking, and the console's answer is unverified. Destroying a rwlock while another thread is acquiring it is not guarded