feat(libult): add libsceult impl

This commit is contained in:
boykopovar
2026-09-07 11:37:55 +03:00
parent 7592031dc9
commit 5ae2a810e9
3 changed files with 455 additions and 152 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ Converter for native execution of PlayStation 5 ELF binaries on Linux through bi
## Status
Execution reaches `_start`, stack unwinding and exception handling tables are built. All unimplemented stub functions throw std::runtime_error. `what()` is printed to stderr and the process terminates. Now: `sceUltInitialize not implemented`
Execution reaches `_start`, stack unwinding and exception handling tables are built. All unimplemented stub functions throw std::runtime_error. `what()` is printed to stderr and the process terminates. Now: `sceSystemServiceHideSplashScreen not implemented`
## Disclaimer
+393 -151
View File
@@ -1,199 +1,441 @@
#include <cstdint>
#include <atomic>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <deque>
#include <memory>
#include <mutex>
#include <unordered_map>
#include <vector>
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
#include "SceUltTypes.hpp"
extern "C" int scePthreadCreate(Pthread* thread, const PthreadAttr* attr, void* (*entry)(void*), void* arg, const char* name);
extern "C" int scePthreadJoin(Pthread thread, void** retval);
namespace {
std::mutex gMutex;
std::unordered_map<void*, std::shared_ptr<UltMutexState>> gMutexes;
std::unordered_map<void*, std::shared_ptr<UltSemaphoreState>> gSemaphores;
std::unordered_map<void*, UltResourcePoolState> gResourcePools;
std::unordered_map<void*, UltQueueDataPoolState> gQueueDataPools;
std::unordered_map<void*, std::shared_ptr<UltQueueState>> gQueues;
std::unordered_map<void*, UltRuntimeState> gRuntimes;
std::unordered_map<void*, std::shared_ptr<UltUlthreadState>> gUlthreads;
std::uint64_t alignUp(std::uint64_t value, std::uint64_t alignment) {
return (value + alignment - 1u) & ~(alignment - 1u);
}
int semaphoreGetState(void* semaphore, std::shared_ptr<UltSemaphoreState>* out) {
std::lock_guard<std::mutex> lock(gMutex);
auto it = gSemaphores.find(semaphore);
if (it == gSemaphores.end()) {
return semaphore == nullptr ? ULT_ERROR_NULL : ULT_ERROR_STATE;
}
*out = it->second;
return ULT_OK;
}
int queueGetState(void* queue, std::shared_ptr<UltQueueState>* out) {
std::lock_guard<std::mutex> lock(gMutex);
auto it = gQueues.find(queue);
if (it == gQueues.end()) {
return queue == nullptr ? ULT_ERROR_NULL : ULT_ERROR_STATE;
}
*out = it->second;
return ULT_OK;
}
void* ulthreadRunner(void* arg) {
auto* state = static_cast<UltUlthreadState*>(arg);
return reinterpret_cast<void*>(static_cast<std::intptr_t>(state->_entry(state->_arg)));
}
}
extern "C" {
int sceUltFinalize(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
int sceUltInitialize() {
return ULT_OK;
}
int sceUltInitialize(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
int sceUltFinalize() {
std::vector<std::shared_ptr<UltSemaphoreState>> semaphores;
{
std::lock_guard<std::mutex> lock(gMutex);
semaphores.reserve(gSemaphores.size());
for (auto& entry : gSemaphores) {
auto& state = entry.second;
std::lock_guard<std::mutex> stateLock(state->_mutex);
state->_alive = false;
semaphores.push_back(state);
}
gSemaphores.clear();
gMutexes.clear();
gResourcePools.clear();
gQueueDataPools.clear();
gQueues.clear();
gRuntimes.clear();
gUlthreads.clear();
}
for (const auto& state : semaphores) {
state->_available.notify_all();
}
return ULT_OK;
}
int sceUltMutexCreate(void* mutex, const char* name, void* waiting_queue_resource_pool, const UltMutexOptParam* opt_param, uint32_t build_version) {
(void)mutex;
(void)name;
(void)waiting_queue_resource_pool;
(void)opt_param;
(void)build_version;
NotImplemented_nid_no_patch(__func__);
return 0;
int sceUltMutexOptParamInitialize(UltMutexOptParam* optParam, std::uint32_t buildVersion) {
(void)buildVersion;
if (optParam == nullptr) {
return ULT_ERROR_NULL;
}
std::memset(optParam, 0, sizeof(*optParam));
return ULT_OK;
}
int sceUltMutexCreate(void* mutex, const char* name, void* waitingQueueResourcePool, const UltMutexOptParam* optParam, std::uint32_t buildVersion) {
(void)name;
(void)buildVersion;
if (mutex == nullptr) {
return ULT_ERROR_NULL;
}
auto state = std::make_shared<UltMutexState>();
state->_attribute = optParam != nullptr ? optParam->attribute : 0;
std::lock_guard<std::mutex> lock(gMutex);
if (waitingQueueResourcePool != nullptr && gResourcePools.find(waitingQueueResourcePool) == gResourcePools.end()) {
return ULT_ERROR_INVALID;
}
std::memset(mutex, 0, 256);
gMutexes[mutex] = std::move(state);
return ULT_OK;
}
int sceUltMutexLock(void* mutex) {
(void)mutex;
NotImplemented_nid_no_patch(__func__);
return 0;
}
int sceUltMutexOptParamInitialize(UltMutexOptParam* opt_param, uint32_t build_version) {
(void)opt_param;
(void)build_version;
NotImplemented_nid_no_patch(__func__);
return 0;
std::shared_ptr<UltMutexState> state;
{
std::lock_guard<std::mutex> lock(gMutex);
auto it = gMutexes.find(mutex);
if (it == gMutexes.end()) {
return mutex == nullptr ? ULT_ERROR_NULL : ULT_ERROR_STATE;
}
state = it->second;
}
state->_mutex.lock();
return ULT_OK;
}
int sceUltMutexUnlock(void* mutex) {
(void)mutex;
NotImplemented_nid_no_patch(__func__);
return 0;
std::shared_ptr<UltMutexState> state;
{
std::lock_guard<std::mutex> lock(gMutex);
auto it = gMutexes.find(mutex);
if (it == gMutexes.end()) {
return mutex == nullptr ? ULT_ERROR_NULL : ULT_ERROR_STATE;
}
state = it->second;
}
state->_mutex.unlock();
return ULT_OK;
}
int sceUltQueueCreate(void* queue, const char* name, uint64_t data_size, void* waiting_queue_resource_pool, void* queue_data_resource_pool, const void* opt_param, uint32_t build_version) {
(void)queue;
(void)name;
(void)data_size;
(void)waiting_queue_resource_pool;
(void)queue_data_resource_pool;
(void)opt_param;
(void)build_version;
NotImplemented_nid_no_patch(__func__);
return 0;
int sceUltWaitingQueueResourcePoolGetWorkAreaSize(std::uint32_t numThreads, std::uint32_t numSyncObjects) {
return static_cast<int>(alignUp(static_cast<std::uint64_t>(numThreads + numSyncObjects) * 256u, 8u));
}
int sceUltQueueDataResourcePoolCreate(void* pool, const char* name, uint32_t num_data, uint64_t data_size, uint32_t num_queue_object, void* waiting_queue_resource_pool, void* work_area, const void* opt_param, uint32_t build_version) {
(void)pool;
(void)name;
(void)num_data;
(void)data_size;
(void)num_queue_object;
(void)waiting_queue_resource_pool;
(void)work_area;
(void)opt_param;
(void)build_version;
NotImplemented_nid_no_patch(__func__);
return 0;
int sceUltWaitingQueueResourcePoolCreate(void* pool, const char* name, std::uint32_t numThreads, std::uint32_t numSyncObjects, void* workArea, const void* optParam, std::uint32_t buildVersion) {
(void)name;
(void)optParam;
(void)buildVersion;
if (pool == nullptr) {
return ULT_ERROR_NULL;
}
std::memset(pool, 0, 256);
std::lock_guard<std::mutex> lock(gMutex);
gResourcePools[pool] = {numThreads, numSyncObjects, workArea};
return ULT_OK;
}
uint64_t sceUltQueueDataResourcePoolGetWorkAreaSize(uint32_t num_data, uint64_t data_size, uint32_t num_queue_object) {
(void)num_data;
(void)data_size;
(void)num_queue_object;
NotImplemented_nid_no_patch(__func__);
return 0;
std::uint64_t sceUltQueueDataResourcePoolGetWorkAreaSize(std::uint32_t numData, std::uint64_t dataSize, std::uint32_t numQueueObject) {
const std::uint64_t dataArea = static_cast<std::uint64_t>(numData) * alignUp(dataSize, 8u);
const std::uint64_t queueArea = static_cast<std::uint64_t>(numQueueObject) * 512u;
return alignUp(dataArea + queueArea, 8u);
}
int sceUltQueueDataResourcePoolCreate(void* pool, const char* name, std::uint32_t numData, std::uint64_t dataSize, std::uint32_t numQueueObject, void* waitingQueueResourcePool, void* workArea, const void* optParam, std::uint32_t buildVersion) {
(void)name;
(void)optParam;
(void)buildVersion;
if (pool == nullptr) {
return ULT_ERROR_NULL;
}
std::lock_guard<std::mutex> lock(gMutex);
if (waitingQueueResourcePool != nullptr && gResourcePools.find(waitingQueueResourcePool) == gResourcePools.end()) {
return ULT_ERROR_INVALID;
}
std::memset(pool, 0, 512);
gQueueDataPools[pool] = {numData, dataSize, numQueueObject, waitingQueueResourcePool, workArea};
return ULT_OK;
}
int sceUltQueueCreate(void* queue, const char* name, std::uint64_t dataSize, void* waitingQueueResourcePool, void* queueDataResourcePool, const void* optParam, std::uint32_t buildVersion) {
(void)name;
(void)optParam;
(void)buildVersion;
if (queue == nullptr) {
return ULT_ERROR_NULL;
}
auto state = std::make_shared<UltQueueState>();
state->_dataSize = dataSize;
state->_waitingPool = waitingQueueResourcePool;
state->_dataPool = queueDataResourcePool;
std::lock_guard<std::mutex> lock(gMutex);
auto dataPoolIt = gQueueDataPools.find(queueDataResourcePool);
if (dataPoolIt == gQueueDataPools.end()) {
return ULT_ERROR_INVALID;
}
if (waitingQueueResourcePool != nullptr && gResourcePools.find(waitingQueueResourcePool) == gResourcePools.end()) {
return ULT_ERROR_INVALID;
}
state->_capacity = dataPoolIt->second._numData;
std::memset(queue, 0, 512);
gQueues[queue] = std::move(state);
return ULT_OK;
}
int sceUltQueuePush(void* queue, const void* data) {
(void)queue;
(void)data;
NotImplemented_nid_no_patch(__func__);
return 0;
std::shared_ptr<UltQueueState> state;
if (int ret = queueGetState(queue, &state); ret != ULT_OK) {
return ret;
}
if (data == nullptr && state->_dataSize != 0) {
return ULT_ERROR_NULL;
}
std::lock_guard<std::mutex> lock(state->_mutex);
if (state->_capacity != 0 && state->_items.size() >= state->_capacity) {
return ULT_OK;
}
auto& item = state->_items.emplace_back(static_cast<std::size_t>(state->_dataSize));
if (!item.empty()) {
std::memcpy(item.data(), data, item.size());
}
return ULT_OK;
}
int sceUltQueueTryPop(void* queue, void* data) {
(void)queue;
(void)data;
NotImplemented_nid_no_patch(__func__);
return 0;
std::shared_ptr<UltQueueState> state;
if (int ret = queueGetState(queue, &state); ret != ULT_OK) {
return ret;
}
if (data == nullptr && state->_dataSize != 0) {
return ULT_ERROR_NULL;
}
std::lock_guard<std::mutex> lock(state->_mutex);
if (state->_items.empty()) {
return ULT_ERROR_AGAIN;
}
auto item = std::move(state->_items.front());
state->_items.pop_front();
if (!item.empty()) {
std::memcpy(data, item.data(), item.size());
}
return ULT_OK;
}
int sceUltSemaphoreAcquire(void* semaphore, int32_t num_resource) {
(void)semaphore;
(void)num_resource;
NotImplemented_nid_no_patch(__func__);
return 0;
int sceUltSemaphoreCreate(void* semaphore, const char* name, std::int32_t numInitialResource, void* waitingQueueResourcePool, const void* optParam, std::uint32_t buildVersion) {
(void)name;
(void)optParam;
(void)buildVersion;
if (semaphore == nullptr) {
return ULT_ERROR_NULL;
}
if ((reinterpret_cast<std::uintptr_t>(semaphore) & 7u) != 0) {
return ULT_ERROR_ALIGNMENT;
}
if (numInitialResource < 0) {
return ULT_ERROR_RANGE;
}
auto state = std::make_shared<UltSemaphoreState>();
state->_resources = numInitialResource;
std::lock_guard<std::mutex> lock(gMutex);
if (waitingQueueResourcePool != nullptr && gResourcePools.find(waitingQueueResourcePool) == gResourcePools.end()) {
return ULT_ERROR_INVALID;
}
if (gSemaphores.find(semaphore) != gSemaphores.end()) {
return ULT_ERROR_STATE;
}
std::memset(semaphore, 0, 256);
gSemaphores[semaphore] = std::move(state);
return ULT_OK;
}
int sceUltSemaphoreCreate(void* semaphore, const char* name, int32_t num_initial_resource, void* waiting_queue_resource_pool, const void* opt_param, uint32_t build_version) {
(void)semaphore;
(void)name;
(void)num_initial_resource;
(void)waiting_queue_resource_pool;
(void)opt_param;
(void)build_version;
NotImplemented_nid_no_patch(__func__);
return 0;
int sceUltSemaphoreAcquire(void* semaphore, std::int32_t numResource) {
if (numResource <= 0) {
return ULT_ERROR_RANGE;
}
std::shared_ptr<UltSemaphoreState> state;
if (int ret = semaphoreGetState(semaphore, &state); ret != ULT_OK) {
return ret;
}
std::unique_lock<std::mutex> lock(state->_mutex);
if (!state->_alive) {
return ULT_ERROR_STATE;
}
state->_waiters++;
state->_available.wait(lock, [&] { return !state->_alive || state->_resources >= numResource; });
state->_waiters--;
if (!state->_alive) {
return ULT_ERROR_STATE;
}
state->_resources -= numResource;
return ULT_OK;
}
int sceUltSemaphoreTryAcquire(void* semaphore, std::int32_t numResource) {
if (numResource <= 0) {
return ULT_ERROR_RANGE;
}
std::shared_ptr<UltSemaphoreState> state;
if (int ret = semaphoreGetState(semaphore, &state); ret != ULT_OK) {
return ret;
}
std::lock_guard<std::mutex> lock(state->_mutex);
if (!state->_alive) {
return ULT_ERROR_STATE;
}
if (state->_resources < numResource) {
return ULT_ERROR_AGAIN;
}
state->_resources -= numResource;
return ULT_OK;
}
int sceUltSemaphoreRelease(void* semaphore, std::int32_t numResource) {
if (numResource <= 0) {
return ULT_ERROR_RANGE;
}
std::shared_ptr<UltSemaphoreState> state;
if (int ret = semaphoreGetState(semaphore, &state); ret != ULT_OK) {
return ret;
}
{
std::lock_guard<std::mutex> lock(state->_mutex);
if (!state->_alive) {
return ULT_ERROR_STATE;
}
if (state->_resources > INT32_MAX - numResource) {
return ULT_ERROR_RANGE;
}
state->_resources += numResource;
}
state->_available.notify_all();
return ULT_OK;
}
int sceUltSemaphoreDestroy(void* semaphore) {
(void)semaphore;
NotImplemented_nid_no_patch(__func__);
return 0;
if (semaphore == nullptr) {
return ULT_ERROR_NULL;
}
std::lock_guard<std::mutex> globalLock(gMutex);
auto it = gSemaphores.find(semaphore);
if (it == gSemaphores.end()) {
return ULT_ERROR_STATE;
}
auto state = it->second;
std::lock_guard<std::mutex> stateLock(state->_mutex);
if (state->_waiters != 0) {
return ULT_ERROR_BUSY;
}
state->_alive = false;
gSemaphores.erase(it);
return ULT_OK;
}
int sceUltSemaphoreRelease(void* semaphore, int32_t num_resource) {
(void)semaphore;
(void)num_resource;
NotImplemented_nid_no_patch(__func__);
return 0;
int sceUltUlthreadRuntimeOptParamInitialize(UltUlthreadRuntimeOptParam* optParam, std::uint32_t buildVersion) {
(void)buildVersion;
if (optParam == nullptr) {
return ULT_ERROR_NULL;
}
std::memset(optParam, 0, sizeof(*optParam));
return ULT_OK;
}
int sceUltSemaphoreTryAcquire(void* semaphore, int32_t num_resource) {
(void)semaphore;
(void)num_resource;
NotImplemented_nid_no_patch(__func__);
return 0;
std::uint64_t sceUltUlthreadRuntimeGetWorkAreaSize(std::uint32_t maxNumUlthread, std::uint32_t numWorkerThread) {
return alignUp(static_cast<std::uint64_t>(maxNumUlthread) * 256u + static_cast<std::uint64_t>(numWorkerThread) * 16u * 1024u, 8u);
}
int sceUltUlthreadCreate(void* ulthread, const char* name, UltUlthreadEntry entry, uint64_t arg, void* context, uint64_t size_context, void* runtime, const void* opt_param, uint32_t build_version) {
(void)ulthread;
(void)name;
(void)entry;
(void)arg;
(void)context;
(void)size_context;
(void)runtime;
(void)opt_param;
(void)build_version;
NotImplemented_nid_no_patch(__func__);
return 0;
int sceUltUlthreadRuntimeCreate(void* runtime, const char* name, std::uint32_t maxNumUlthread, std::uint32_t numWorkerThread, void* workArea, const void* optParam, std::uint32_t buildVersion) {
(void)name;
(void)optParam;
(void)buildVersion;
if (runtime == nullptr) {
return ULT_ERROR_NULL;
}
std::memset(runtime, 0, 4096);
std::lock_guard<std::mutex> lock(gMutex);
gRuntimes[runtime] = {maxNumUlthread, numWorkerThread, workArea};
return ULT_OK;
}
int sceUltUlthreadJoin(void* ulthread, int32_t* status) {
(void)ulthread;
(void)status;
NotImplemented_nid_no_patch(__func__);
return 0;
int sceUltUlthreadCreate(void* ulthread, const char* name, UltUlthreadEntry entry, std::uint64_t arg, void* context, std::uint64_t sizeContext, void* runtime, const void* optParam, std::uint32_t buildVersion) {
(void)context;
(void)sizeContext;
(void)optParam;
(void)buildVersion;
if (ulthread == nullptr || entry == nullptr || runtime == nullptr) {
return ULT_ERROR_NULL;
}
auto state = std::make_shared<UltUlthreadState>();
state->_entry = entry;
state->_arg = arg;
{
std::lock_guard<std::mutex> lock(gMutex);
if (gRuntimes.find(runtime) == gRuntimes.end()) {
return ULT_ERROR_STATE;
}
if (gUlthreads.find(ulthread) != gUlthreads.end()) {
return ULT_ERROR_STATE;
}
std::memset(ulthread, 0, 512);
gUlthreads[ulthread] = state;
}
const int result = scePthreadCreate(&state->_thread, nullptr, ulthreadRunner, state.get(), name != nullptr ? name : "");
if (result != 0) {
std::lock_guard<std::mutex> lock(gMutex);
gUlthreads.erase(ulthread);
return ULT_ERROR_AGAIN;
}
return ULT_OK;
}
int sceUltUlthreadRuntimeCreate(void* runtime, const char* name, uint32_t max_num_ulthread, uint32_t num_worker_thread, void* work_area, const void* opt_param, uint32_t build_version) {
(void)runtime;
(void)name;
(void)max_num_ulthread;
(void)num_worker_thread;
(void)work_area;
(void)opt_param;
(void)build_version;
NotImplemented_nid_no_patch(__func__);
return 0;
}
uint64_t sceUltUlthreadRuntimeGetWorkAreaSize(uint32_t max_num_ulthread, uint32_t num_worker_thread) {
(void)max_num_ulthread;
(void)num_worker_thread;
NotImplemented_nid_no_patch(__func__);
return 0;
}
int sceUltUlthreadRuntimeOptParamInitialize(UltUlthreadRuntimeOptParam* opt_param, uint32_t build_version) {
(void)opt_param;
(void)build_version;
NotImplemented_nid_no_patch(__func__);
return 0;
}
int sceUltWaitingQueueResourcePoolCreate(void* pool, const char* name, uint32_t num_threads, uint32_t num_sync_objects, void* work_area, const void* opt_param, uint32_t build_version) {
(void)pool;
(void)name;
(void)num_threads;
(void)num_sync_objects;
(void)work_area;
(void)opt_param;
(void)build_version;
NotImplemented_nid_no_patch(__func__);
return 0;
}
uint64_t sceUltWaitingQueueResourcePoolGetWorkAreaSize(uint32_t num_threads, uint32_t num_sync_objects) {
(void)num_threads;
(void)num_sync_objects;
NotImplemented_nid_no_patch(__func__);
return 0;
int sceUltUlthreadJoin(void* ulthread, std::int32_t* status) {
if (ulthread == nullptr) {
return ULT_ERROR_NULL;
}
std::shared_ptr<UltUlthreadState> state;
{
std::lock_guard<std::mutex> lock(gMutex);
auto it = gUlthreads.find(ulthread);
if (it == gUlthreads.end()) {
return ULT_ERROR_STATE;
}
state = it->second;
}
void* result = nullptr;
if (scePthreadJoin(state->_thread, &result) != 0) {
return ULT_ERROR_STATE;
}
if (status != nullptr) {
*status = static_cast<std::int32_t>(reinterpret_cast<std::intptr_t>(result));
}
std::lock_guard<std::mutex> lock(gMutex);
gUlthreads.erase(ulthread);
return ULT_OK;
}
}
+61
View File
@@ -0,0 +1,61 @@
#ifndef CORE_LIBS_PRX_LIBSCEULT_SCEULTTYPES_HPP
#define CORE_LIBS_PRX_LIBSCEULT_SCEULTTYPES_HPP
constexpr int ULT_OK = 0;
constexpr int ULT_ERROR_NULL = -2139029503;
constexpr int ULT_ERROR_ALIGNMENT = -2139029502;
constexpr int ULT_ERROR_RANGE = -2139029501;
constexpr int ULT_ERROR_INVALID = -2139029500;
constexpr int ULT_ERROR_STATE = -2139029498;
constexpr int ULT_ERROR_BUSY = -2139029497;
constexpr int ULT_ERROR_AGAIN = -2139029496;
struct UltMutexState {
std::recursive_mutex _mutex;
std::uint32_t _attribute = 0;
};
struct UltSemaphoreState {
std::mutex _mutex;
std::condition_variable _available;
std::int32_t _resources = 0;
std::uint32_t _waiters = 0;
bool _alive = true;
};
struct UltResourcePoolState {
std::uint32_t _numThreads = 0;
std::uint32_t _numSyncObjects = 0;
void* _workArea = nullptr;
};
struct UltQueueDataPoolState {
std::uint32_t _numData = 0;
std::uint64_t _dataSize = 0;
std::uint32_t _numQueueObject = 0;
void* _waitingPool = nullptr;
void* _workArea = nullptr;
};
struct UltQueueState {
std::mutex _mutex;
std::deque<std::vector<std::uint8_t>> _items;
std::uint64_t _dataSize = 0;
std::uint32_t _capacity = 0;
void* _waitingPool = nullptr;
void* _dataPool = nullptr;
};
struct UltRuntimeState {
std::uint32_t _maxNumUlthread = 0;
std::uint32_t _numWorkerThread = 0;
void* _workArea = nullptr;
};
struct UltUlthreadState {
UltUlthreadEntry _entry = nullptr;
std::uint64_t _arg = 0;
Pthread _thread = nullptr;
};
#endif