Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
024ccc1
feat(libkernel): let other libraries observe closed guest descriptors
DavidSamir Oct 21, 2026
19e1af6
feat(libkernel_sys): implement the unnamed ksem semaphores
DavidSamir Oct 21, 2026
5f8cd77
test(libkernel_sys): cover the unnamed ksem semaphores, including clo…
DavidSamir Oct 21, 2026
8f56721
docs(debt): record the ksem descriptor limits in libkernel_sys
DavidSamir Oct 21, 2026
60d4f0d
feat(libkernel_sys): implement POSIX message queues
DavidSamir Oct 21, 2026
c3861fb
test(libkernel_sys): cover message queue open, send, receive, attribu…
DavidSamir Oct 21, 2026
d689697
docs(debt): record the libkernel_sys message queue limits
DavidSamir Oct 21, 2026
738379e
feat(libkernel_sys): implement kmq_notify registration
DavidSamir Oct 21, 2026
2d64dc6
test(libkernel_sys): cover kmq_notify registration, consumption and r…
DavidSamir Oct 21, 2026
9241cdb
docs(debt): record that libkernel_sys kmq_notify cannot deliver signals
DavidSamir Oct 21, 2026
38fd999
test(libkernel_sys): open the ksem stale-descriptor file by a guest-r…
DavidSamir Oct 21, 2026
bd708c8
Merge branch 'feat/libkernel-sys-ksem-unnamed' into feat/libkernel-sy…
DavidSamir Oct 21, 2026
d821664
test(libkernel_sys): open the kmq stale-descriptor file by a guest-re…
DavidSamir Oct 21, 2026
63cd244
Merge branch 'feat/libkernel-sys-mqueue' into feat/libkernel-sys-kmq-…
DavidSamir Oct 21, 2026
8db8924
refactor(libkernel): move the unnamed ksem calls into libkernel
DavidSamir Oct 21, 2026
6566109
Merge feat/libkernel-sys-ksem-unnamed: the unnamed ksem calls moved i…
DavidSamir Oct 21, 2026
caf88d1
Merge branch 'feat/libkernel-sys-mqueue' into feat/libkernel-sys-kmq-…
DavidSamir Oct 21, 2026
b0d85e3
docs(debt): list the unnamed ksem calls under Functional
DavidSamir Oct 21, 2026
d07deaa
Merge branch 'feat/libkernel-sys-ksem-unnamed' into feat/libkernel-sy…
DavidSamir Oct 21, 2026
c045c1e
Merge branch 'feat/libkernel-sys-mqueue' into feat/libkernel-sys-kmq-…
DavidSamir Oct 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions core/libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,17 @@ if(BUILD_TESTING)
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceSsl>")
endif()

add_test_executable(guest_kernel_sys_ksem_tests tests/GuestKernelSysKsem.cpp)
target_include_directories(guest_kernel_sys_ksem_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(guest_kernel_sys_ksem_tests PRIVATE libkernel_sys libkernel libc)
configure_windows_unwind(guest_kernel_sys_ksem_tests)
add_test(NAME guest_kernel_sys_ksem COMMAND guest_kernel_sys_ksem_tests)
set_tests_properties(guest_kernel_sys_ksem PROPERTIES TIMEOUT 30)
if(WIN32)
set_tests_properties(guest_kernel_sys_ksem PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel_sys>")
endif()

add_test_executable(guest_system_gesture_tests tests/GuestSystemGesture.cpp)
target_include_directories(guest_system_gesture_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(guest_system_gesture_tests PRIVATE libSceSystemGesture libc)
Expand All @@ -528,6 +539,17 @@ if(BUILD_TESTING)
add_subdirectory(tests/register_context)
endif()

add_test_executable(guest_kernel_sys_mqueue_tests tests/GuestKernelSysMqueue.cpp)
target_include_directories(guest_kernel_sys_mqueue_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(guest_kernel_sys_mqueue_tests PRIVATE libkernel_sys libkernel libc)
configure_windows_unwind(guest_kernel_sys_mqueue_tests)
add_test(NAME guest_kernel_sys_mqueue COMMAND guest_kernel_sys_mqueue_tests)
set_tests_properties(guest_kernel_sys_mqueue PROPERTIES TIMEOUT 30)
if(WIN32)
set_tests_properties(guest_kernel_sys_mqueue PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel_sys>")
endif()

add_test_executable(savedata_memory_metadata_tests tests/SaveDataMemoryMetadata.cpp)
target_include_directories(savedata_memory_metadata_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(savedata_memory_metadata_tests PRIVATE libSceSaveData.native)
Expand Down
1 change: 1 addition & 0 deletions core/libs/prx/libkernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ add_library(libkernel SHARED EXCLUDE_FROM_ALL
${CMAKE_CURRENT_SOURCE_DIR}/File/src/SandboxCertificates.cpp
${CMAKE_CURRENT_SOURCE_DIR}/File/src/Stdio.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../libc/src/Rename.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Semaphore/src/Ksem.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Semaphore/src/Semaphore.cpp
${CMAKE_CURRENT_SOURCE_DIR}/SyncOnAddress/src/SyncOnAddress.cpp
${CMAKE_CURRENT_SOURCE_DIR}/DirectMemory/Export.cpp
Expand Down
18 changes: 18 additions & 0 deletions core/libs/prx/libkernel/Equeue/Equeue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,26 @@ KernelEqueueEvent EqueueDescriptorEvent(int descriptor, int16_t filter, uint16_t
return event;
}

static std::mutex g_closeObserverMutex;
static std::vector<void (*)(int)> g_closeObservers;

extern "C" void RegisterDescriptorCloseObserver_nid_no_patch(void (*observer)(int descriptor)) {
std::lock_guard lock(g_closeObserverMutex);
g_closeObservers.push_back(observer);
}

static void NotifyDescriptorCloseObservers(int descriptor) {
std::vector<void (*)(int)> observers;
{
std::lock_guard lock(g_closeObserverMutex);
observers = g_closeObservers;
}
for (const auto observer : observers) observer(descriptor);
}

void EqueueDescriptorClosed(int descriptor) {
KqueueDescriptorClosed(descriptor);
NotifyDescriptorCloseObservers(descriptor);
std::vector<KernelEqueueRef> owners;
{
std::unique_lock lock(g_equeueMutex);
Expand Down
3 changes: 3 additions & 0 deletions core/libs/prx/libkernel/File/src/Stdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "prx/libc/include/AppArchive.hpp"
#include "prx/libkernel/KernelErrors.hpp"
#include "prx/libkernel/Equeue/Equeue.hpp"
#include "prx/libkernel/Semaphore/include/Ksem.hpp"
#include "prx/libkernel/Socket/include/SocketRuntime.hpp"
#include "prx/libkernel/Socket/include/SocketPoll.hpp"
#include <atomic>
Expand Down Expand Up @@ -761,6 +762,7 @@ int APS5_VABI dup_nid_postfix(int d) {
if (GuestSockets::IsOpen(d)) return GuestSockets::Duplicate(d);
if (d < 0) return PosixFailure(GUEST_EBADF);
if (IsKqueueDescriptor(d)) throw std::runtime_error(std::string(__func__) + ": duplicating a kqueue descriptor is not supported");
if (IsKsemDescriptor(d)) throw std::runtime_error(std::string(__func__) + ": duplicating a ksem id is not supported");
#ifdef _WIN32
const int duplicate = WithoutParameterHandler([d] { return ::_dup(d); });
if (duplicate >= 0) File::DuplicateDirectoryDescriptor(d, duplicate);
Expand Down Expand Up @@ -791,6 +793,7 @@ int APS5_VABI dup2_nid_postfix(int from, int to) {
}
const bool sourceIsRandom = File::IsRandomDevice(from);
if (from != to && IsKqueueDescriptor(from)) throw std::runtime_error(std::string(__func__) + ": duplicating a kqueue descriptor is not supported");
if (from != to && IsKsemDescriptor(from)) throw std::runtime_error(std::string(__func__) + ": duplicating a ksem id is not supported");
#ifdef _WIN32
if (WithoutParameterHandler([from] { return ::_get_osfhandle(from) == -1 ? -1 : 0; }) != 0) return PosixFailure(GUEST_EBADF);
if (from == to) return to;
Expand Down
6 changes: 6 additions & 0 deletions core/libs/prx/libkernel/Semaphore/include/Ksem.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef CORE_LIBS_PRX_LIBKERNEL_SEMAPHORE_KSEM_HPP
#define CORE_LIBS_PRX_LIBKERNEL_SEMAPHORE_KSEM_HPP

bool IsKsemDescriptor(int descriptor);

#endif
175 changes: 175 additions & 0 deletions core/libs/prx/libkernel/Semaphore/src/Ksem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libkernel/Semaphore/include/Ksem.hpp"
#include <chrono>
#include <climits>
#include <condition_variable>
#include <cstdint>
#include <map>
#include <memory>
#include <mutex>
#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#else
#include <fcntl.h>
#include <unistd.h>
#endif

extern "C" int* APS5_VABI __error_nid_postfix();
extern "C" int APS5_VABI close_nid_postfix(int descriptor);
extern "C" void RegisterDescriptorCloseObserver_nid_no_patch(void (*observer)(int descriptor));

namespace {

constexpr int GuestEfault = 14;
constexpr int GuestEbusy = 16;
constexpr int GuestEinval = 22;
constexpr int GuestEnospc = 28;
constexpr int GuestEagain = 35;
constexpr int GuestEtimedout = 60;
constexpr int GuestEoverflow = 84;
constexpr unsigned SemValueMax = 0x7fffffffu;
constexpr std::size_t SemMax = 30;

struct GuestTimespec {
std::int64_t seconds;
std::int64_t nanoseconds;
};

struct Ksem {
std::mutex lock;
std::condition_variable available;
unsigned count = 0;
unsigned waiters = 0;
};

std::mutex g_ksemLock;
std::map<int, std::shared_ptr<Ksem>> g_ksems;

int Fail(int error) {
*__error_nid_postfix() = error;
return -1;
}

std::shared_ptr<Ksem> Find(std::intptr_t id) {
if (id < 0 || id > INT_MAX) return {};
std::lock_guard lock(g_ksemLock);
const auto found = g_ksems.find(static_cast<int>(id));
return found != g_ksems.end() ? found->second : std::shared_ptr<Ksem>{};
}

int ReserveDescriptor() {
#ifdef _WIN32
return ::_open("NUL", _O_RDONLY | _O_BINARY);
#else
return ::open("/dev/null", O_RDONLY | O_CLOEXEC);
#endif
}

void DescriptorClosed(int descriptor) {
std::lock_guard lock(g_ksemLock);
g_ksems.erase(descriptor);
}

const bool g_observerRegistered = [] {
RegisterDescriptorCloseObserver_nid_no_patch(DescriptorClosed);
return true;
}();

int Wait(std::intptr_t id, bool once, const GuestTimespec* abstime) {
const auto ksem = Find(id);
if (!ksem) return Fail(GuestEinval);
std::unique_lock lock(ksem->lock);
if (ksem->count == 0) {
if (once) return Fail(GuestEagain);
++ksem->waiters;
bool ready = true;
if (abstime == nullptr) {
ksem->available.wait(lock, [&] { return ksem->count > 0; });
} else {
const auto deadline = std::chrono::system_clock::time_point(std::chrono::duration_cast<std::chrono::system_clock::duration>(
std::chrono::seconds(abstime->seconds) + std::chrono::nanoseconds(abstime->nanoseconds)));
ready = ksem->available.wait_until(lock, deadline, [&] { return ksem->count > 0; });
}
--ksem->waiters;
if (!ready) return Fail(GuestEtimedout);
}
--ksem->count;
return 0;
}

}

bool IsKsemDescriptor(int descriptor) {
std::lock_guard lock(g_ksemLock);
return g_ksems.count(descriptor) != 0;
}

extern "C" {

int APS5_VABI ksem_init_nid_postfix(std::intptr_t* idp, unsigned int value) {
if (value > SemValueMax) return Fail(GuestEinval);
const int descriptor = ReserveDescriptor();
if (descriptor < 0) return Fail(GuestEnospc);
if (idp == nullptr) {
close_nid_postfix(descriptor);
return Fail(GuestEfault);
}
*idp = descriptor;
auto ksem = std::make_shared<Ksem>();
ksem->count = value;
{
std::lock_guard lock(g_ksemLock);
if (g_ksems.size() < SemMax) {
g_ksems[descriptor] = std::move(ksem);
return 0;
}
}
close_nid_postfix(descriptor);
return Fail(GuestEnospc);
}

int APS5_VABI ksem_post_nid_postfix(std::intptr_t id) {
const auto ksem = Find(id);
if (!ksem) return Fail(GuestEinval);
std::lock_guard lock(ksem->lock);
if (ksem->count == SemValueMax) return Fail(GuestEoverflow);
++ksem->count;
ksem->available.notify_one();
return 0;
}

int APS5_VABI ksem_wait_nid_postfix(std::intptr_t id) {
return Wait(id, false, nullptr);
}

int APS5_VABI ksem_trywait_nid_postfix(std::intptr_t id) {
return Wait(id, true, nullptr);
}

int APS5_VABI ksem_timedwait_nid_postfix(std::intptr_t id, const GuestTimespec* abstime) {
if (abstime != nullptr && (abstime->nanoseconds < 0 || abstime->nanoseconds >= 1000000000)) return Fail(GuestEinval);
return Wait(id, false, abstime);
}

int APS5_VABI ksem_getvalue_nid_postfix(std::intptr_t id, int* value) {
const auto ksem = Find(id);
if (!ksem) return Fail(GuestEinval);
if (value == nullptr) return Fail(GuestEfault);
std::lock_guard lock(ksem->lock);
*value = static_cast<int>(ksem->count);
return 0;
}

int APS5_VABI ksem_destroy_nid_postfix(std::intptr_t id) {
const auto ksem = Find(id);
if (!ksem) return Fail(GuestEinval);
{
std::lock_guard lock(ksem->lock);
if (ksem->waiters != 0) return Fail(GuestEbusy);
}
return close_nid_postfix(static_cast<int>(id));
}

}
1 change: 1 addition & 0 deletions core/libs/prx/libkernel_sys/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_library(${LIB_NAME} SHARED EXCLUDE_FROM_ALL Export.cpp Export.Discovery.cpp)
target_include_directories(${LIB_NAME} PRIVATE ${LIBS_INCLUDE_DIR})
target_sources(${LIB_NAME} PRIVATE Mqueue.cpp)
configure_windows_unwind(${LIB_NAME})
65 changes: 0 additions & 65 deletions core/libs/prx/libkernel_sys/Export.Discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,86 +198,21 @@ int APS5_VABI kldunload_nid_postfix(void) {
return 0;
}

int APS5_VABI kmq_notify_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI kmq_open_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI kmq_setattr_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI kmq_timedreceive_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI kmq_timedsend_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI kmq_unlink_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI ksem_close_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI ksem_destroy_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI ksem_getvalue_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI ksem_init_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI ksem_open_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI ksem_post_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI ksem_timedwait_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI ksem_trywait_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI ksem_unlink_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI ksem_wait_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI ktimer_create_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
Expand Down
Loading
Loading