diff --git a/core/libs/prx/libkernel/System/src/Signals.cpp b/core/libs/prx/libkernel/System/src/Signals.cpp index 0e06f153836a..38765615fbf6 100644 --- a/core/libs/prx/libkernel/System/src/Signals.cpp +++ b/core/libs/prx/libkernel/System/src/Signals.cpp @@ -307,6 +307,21 @@ int APS5_VABI pause_nid_postfix(void) { NotImplemented_nid_no_patch("pause: waiting for a delivered signal"); return -1; } + +int APS5_VABI sigpending_nid_postfix(GuestSignalSet* set) { + if (set == nullptr) { *__error_nid_postfix() = 14; return -1; } + for (auto& word : set->bits) word = 0; + return 0; +} + +int APS5_VABI sigsuspend_nid_postfix(const GuestSignalSet* mask) { + if (mask == nullptr) { *__error_nid_postfix() = 14; return -1; } + char message[160]; + std::snprintf(message, sizeof(message), "sigsuspend: waiting for a delivered signal (mask 0x%x 0x%x 0x%x 0x%x)", + mask->bits[0], mask->bits[1], mask->bits[2], mask->bits[3]); + NotImplemented_nid_no_patch(message); + return -1; +} } extern "C" { diff --git a/core/libs/tests/GuestSignals.cpp b/core/libs/tests/GuestSignals.cpp index b0368a76e67b..043fbe5be1eb 100644 --- a/core/libs/tests/GuestSignals.cpp +++ b/core/libs/tests/GuestSignals.cpp @@ -7,6 +7,9 @@ #include #include using Handler = void (APS5_VABI *)(int); +struct GuestSignalSet { + std::uint32_t bits[4]; +}; extern "C" { Handler APS5_VABI signal_nid_postfix(int, Handler); int APS5_VABI raise_nid_postfix(int); @@ -18,10 +21,9 @@ int APS5_VABI pthread_sigmask_nid_postfix(int, const void*, void*); int* APS5_VABI __error_nid_postfix(); int APS5_VABI _is_signal_return_nid_postfix(std::uint64_t); unsigned int APS5_VABI GuestAlarm_nid_no_patch(unsigned int); +int APS5_VABI sigpending_nid_postfix(GuestSignalSet*); +int APS5_VABI sigsuspend_nid_postfix(const GuestSignalSet*); } -struct GuestSignalSet { - std::uint32_t bits[4]; -}; struct GuestSigaction { std::uintptr_t handler; int flags; @@ -202,4 +204,23 @@ int main() { Require(sigaction_nid_postfix(13, nullptr, ¤t) == 0 && current.handler == static_cast(-1) && current.flags == 0x2); Require(signal_nid_postfix(13, nullptr) == invalid); Require(sigaction_nid_postfix(13, nullptr, ¤t) == 0 && current.handler == 0); + *__error_nid_postfix() = 0; + Require(sigpending_nid_postfix(nullptr) == -1); + Require(*__error_nid_postfix() == 14); + *__error_nid_postfix() = 42; + GuestSignalSet pendingSignals{{0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}}; + Require(sigpending_nid_postfix(&pendingSignals) == 0); + Require(pendingSignals.bits[0] == 0 && pendingSignals.bits[1] == 0 && pendingSignals.bits[2] == 0 && pendingSignals.bits[3] == 0); + Require(*__error_nid_postfix() == 42); + *__error_nid_postfix() = 0; + Require(sigsuspend_nid_postfix(nullptr) == -1); + Require(*__error_nid_postfix() == 14); + GuestSignalSet suspendMask{{0x1, 0, 0, 0}}; + bool thrown = false; + try { + sigsuspend_nid_postfix(&suspendMask); + } catch (const std::runtime_error& error) { + thrown = std::string(error.what()) == "sigsuspend: waiting for a delivered signal (mask 0x1 0x0 0x0 0x0) not implemented"; + } + Require(thrown); } diff --git a/docs/dev/debt/libkernel.md b/docs/dev/debt/libkernel.md index 132b6b9b4218..94a159239a31 100644 --- a/docs/dev/debt/libkernel.md +++ b/docs/dev/debt/libkernel.md @@ -118,6 +118,7 @@ - [sceKernelRaiseException](../../../core/libs/prx/libkernel/System/src/Exception.cpp) reaches a thread that waits in an alertable `TimedWait` wait through an APC. A thread blocked anywhere else, including all waits under `APS5_COARSE_TIMED_WAITS` and host waits inside the emulator, is redirected and runs the handler only when that wait returns - [sceKernelRaiseException](../../../core/libs/prx/libkernel/System/src/Exception.cpp) on Windows runs the handler wherever the target thread was interrupted. It waits only while the thread is inside winpthreads or restoring a context through `NtContinue`, and throws if that lasts 1 s, so a handler that calls into other host code the thread was interrupted in, such as the C runtime's heap or a lock of the emulator, can deadlock on it or reenter it - [sigaltstack](../../../core/libs/prx/libkernel/System/src/Signals.cpp) (libkernel) records each thread's alternate signal stack and reports it back, following FreeBSD's `kern_sigaltstack` (`EINVAL` for flags other than `SS_DISABLE`, `ENOMEM` below `MINSIGSTKSZ` 2048), but handlers never run on it, so `SS_ONSTACK` and `EPERM` are never reported +- [sigpending and sigsuspend](../../../core/libs/prx/libkernel/System/src/Signals.cpp) (libkernel) - sigpending clears the returned signal set matching this runtime where Dispatch drops a raised signal while it is blocked rather than queueing it (blocked signals from kill/raise are not held either); sigsuspend checks the mask pointer for null (EFAULT 14) and throws, as pause, sigwait and sigwaitinfo do, naming the mask. - [scePthreadJoin, scePthreadCreate](../../../core/libs/prx/libkernel/Pthread/src/Thread.cpp) (libkernel) - on a PS5 (firmware 13.60) joining a thread a second time returns `ESRCH` (`0x80020003`); here a joined thread is released, so a second join reads freed memory. A null entry crashed the payload and still throws. Joining the calling thread returns `EDEADLK` and a zeroed attribute creates a default thread, as on the console - [Thread cancellation](../../../core/libs/prx/libkernel/Pthread/src/Thread.cpp) (libkernel) follows FreeBSD libthr (states and types 0/1 and 0/2, `PTHREAD_CANCELED` 1, the `_pthread_cleanup_info` node layout), not measured on the console. A request is acted on only in `scePthreadTestcancel`, the condition variable, `sem` and `scePthreadSem` waits, `scePthreadJoin` and the sleeps, and their POSIX names; file and socket calls are not cancellation points. A sleep is not interrupted: the thread exits when it ends. The asynchronous type acts like the deferred one, except at the two set calls. - [cpuset_setaffinity](../../../core/libs/prx/libkernel/Pthread/Posix/Thread.cpp) (libkernel) - only the calling thread or the calling process is handled. Bits outside `sceKernelGetAvailableCpumask` are dropped, and a mask with none of those bits fails with `EDEADLK`, as FreeBSD does when the new set has no CPU in common with the parent set