Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 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 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
254 changes: 254 additions & 0 deletions core/libs/prx/libkernel/Semaphore/src/Ksem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libkernel/Semaphore/include/Ksem.hpp"
#include <cerrno>
#include <chrono>
#include <climits>
#include <condition_variable>
#include <cstdint>
#include <cstring>
#include <map>
#include <memory>
#include <mutex>
#include <set>
#include <string>
#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 GuestEnoent = 2;
constexpr int GuestEfault = 14;
constexpr int GuestEbusy = 16;
constexpr int GuestEexist = 17;
constexpr int GuestEinval = 22;
constexpr int GuestEnfile = 23;
constexpr int GuestEmfile = 24;
constexpr int GuestEnospc = 28;
constexpr int GuestEagain = 35;
constexpr int GuestEtimedout = 60;
constexpr int GuestEnametoolong = 63;
constexpr int GuestEoverflow = 84;
constexpr int GuestOCreat = 0x0200;
constexpr int GuestOExcl = 0x0800;
constexpr std::size_t GuestPathMax = 1024;
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;
bool named = false;
};

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

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
}

std::size_t Objects() {
std::set<const Ksem*> objects;
for (const auto& [descriptor, ksem] : g_ksems) objects.insert(ksem.get());
for (const auto& [path, ksem] : g_named) objects.insert(ksem.get());
return objects.size();
}

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 (Objects() < 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 || ksem->named) return Fail(GuestEinval);
{
std::lock_guard lock(ksem->lock);
if (ksem->waiters != 0) return Fail(GuestEbusy);
}
return close_nid_postfix(static_cast<int>(id));
}

int APS5_VABI ksem_open_nid_postfix(std::intptr_t* idp, const char* name, int oflag, unsigned int mode, unsigned int value) {
(void)mode;
if ((oflag & ~(GuestOCreat | GuestOExcl)) != 0) return Fail(GuestEinval);
if (value > SemValueMax) return Fail(GuestEinval);
const int descriptor = ReserveDescriptor();
if (descriptor < 0) return Fail(errno == ENFILE ? GuestEnfile : GuestEmfile);
const auto fail = [descriptor](int error) {
close_nid_postfix(descriptor);
return Fail(error);
};
if (idp == nullptr) return fail(GuestEfault);
*idp = descriptor;
if (name == nullptr) return fail(GuestEfault);
const std::string path(name, strnlen(name, GuestPathMax));
if (path.size() >= GuestPathMax) return fail(GuestEnametoolong);
if (path[0] != '/') return fail(GuestEinval);
std::unique_lock lock(g_ksemLock);
std::shared_ptr<Ksem> ksem;
const auto found = g_named.find(path);
if (found == g_named.end()) {
if ((oflag & GuestOCreat) == 0) {
lock.unlock();
return fail(GuestEnoent);
}
if (Objects() >= SemMax) {
lock.unlock();
return fail(GuestEnfile);
}
ksem = std::make_shared<Ksem>();
ksem->count = value;
ksem->named = true;
g_named[path] = ksem;
} else {
if ((oflag & (GuestOCreat | GuestOExcl)) == (GuestOCreat | GuestOExcl)) {
lock.unlock();
return fail(GuestEexist);
}
ksem = found->second;
}
g_ksems[descriptor] = std::move(ksem);
return 0;
}

int APS5_VABI ksem_close_nid_postfix(std::intptr_t id) {
const auto ksem = Find(id);
if (!ksem || !ksem->named) return Fail(GuestEinval);
return close_nid_postfix(static_cast<int>(id));
}

int APS5_VABI ksem_unlink_nid_postfix(const char* name) {
if (name == nullptr) return Fail(GuestEfault);
const std::string path(name, strnlen(name, GuestPathMax));
if (path.size() >= GuestPathMax) return Fail(GuestEnametoolong);
std::lock_guard lock(g_ksemLock);
if (g_named.erase(path) == 0) return Fail(GuestEnoent);
return 0;
}

}
50 changes: 0 additions & 50 deletions core/libs/prx/libkernel_sys/Export.Discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,56 +228,6 @@ int APS5_VABI kmq_unlink_nid_postfix(void) {
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