From 940f28d1024bf31b0a01b4cfb660eb102bf5c218 Mon Sep 17 00:00:00 2001 From: OverkillLabs <10800190+OverkillLabs@users.noreply.github.com> Date: Mon, 28 Sep 2026 13:18:07 +0300 Subject: [PATCH 1/4] libc: wide-char formatting and precise sub-tick waits - Add FormattingWide.cpp implementing vswprintf_nid_postfix and wprintf_nid_postfix, the only wide-char printf-family functions upstream had left as unimplemented stubs (Formatting.cpp's checked and unchecked narrow printf/scanf families are unaffected). Removes the dead vswprintf_nid_postfix stub from Export.cpp. - Add PreciseWait.hpp: std::condition_variable::wait_for cannot return in under about one system tick on this MinGW toolchain regardless of the requested budget, even with NtSetTimerResolution; PreciseSleepUs uses a high-resolution waitable timer instead (~0.5ms vs ~15.6ms). Header-only for now: its guest-side callers (Equeue/EventFlag/ Semaphore/AgcDriver wait loops) are out of scope for this change, so it is not wired into them here. --- core/libs/prx/libc/CMakeLists.txt | 1 + core/libs/prx/libc/Export.cpp | 5 +- core/libs/prx/libc/include/PreciseWait.hpp | 51 +++++ core/libs/prx/libc/src/FormattingWide.cpp | 224 +++++++++++++++++++++ 4 files changed, 277 insertions(+), 4 deletions(-) create mode 100644 core/libs/prx/libc/include/PreciseWait.hpp create mode 100644 core/libs/prx/libc/src/FormattingWide.cpp diff --git a/core/libs/prx/libc/CMakeLists.txt b/core/libs/prx/libc/CMakeLists.txt index ba4ae9c93..e6301e575 100644 --- a/core/libs/prx/libc/CMakeLists.txt +++ b/core/libs/prx/libc/CMakeLists.txt @@ -20,6 +20,7 @@ target_sources(libc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/ApplicationProcessParameters.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/JumpBuffer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Formatting.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/FormattingWide.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/RuntimeSupport.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/LocaleSupport.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Process.cpp diff --git a/core/libs/prx/libc/Export.cpp b/core/libs/prx/libc/Export.cpp index 625b27ff9..84b1b05f4 100644 --- a/core/libs/prx/libc/Export.cpp +++ b/core/libs/prx/libc/Export.cpp @@ -81,10 +81,7 @@ int APS5_VABI fscanf_nid_postfix() { return 0; } -int APS5_VABI vswprintf_nid_postfix() { - NotImplemented_nid_no_patch(__func__); - return 0; -} +// vswprintf_nid_postfix is implemented in src/FormattingWide.cpp. APS5_EXPORT("Pu0Ecyk-7FU", libcUnknown_Pu0Ecyk_M7FU); int APS5_VABI libcUnknown_Pu0Ecyk_M7FU() { diff --git a/core/libs/prx/libc/include/PreciseWait.hpp b/core/libs/prx/libc/include/PreciseWait.hpp new file mode 100644 index 000000000..fe1132248 --- /dev/null +++ b/core/libs/prx/libc/include/PreciseWait.hpp @@ -0,0 +1,51 @@ +#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_PRECISEWAIT_HPP +#define CORE_LIBS_PRX_LIBC_INCLUDE_PRECISEWAIT_HPP + +#include +#include + +#ifdef _WIN32 +#include +#endif + +// std::condition_variable::wait_for on this MinGW toolchain cannot return in under about one system tick, +// whatever the budget says: measured 15.6 ms average for a 0 us wait and for a 1 us wait alike, and raising +// the process timer resolution to 0.5 ms with NtSetTimerResolution does not change it. A high-resolution +// waitable timer honours the same request in about 0.5 ms instead. +// +// This matters for any guest-side poll that requests a sub-tick wait budget: without this helper, such a +// poll runs at the OS tick rate rather than at the requested budget, which can turn a tight wait loop into +// a multi-millisecond stall per iteration. +// +// Callers must hold no lock that the awaited event's producer needs: the point of the helper is to wait +// precisely and cheaply, so the caller re-tests its own condition afterwards. +inline constexpr unsigned long long kSubTickWaitUs = 2000; + +inline void PreciseSleepUs(unsigned long long micros) { + if (micros == 0) { + return; + } +#ifdef _WIN32 + struct Timer { + HANDLE handle = CreateWaitableTimerExW(nullptr, nullptr, + CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS); + ~Timer() { + if (handle != nullptr) { + CloseHandle(handle); + } + } + }; + thread_local Timer timer; + if (timer.handle != nullptr) { + LARGE_INTEGER due{}; + due.QuadPart = -static_cast(micros * 10ULL); // relative, 100 ns units + if (SetWaitableTimerEx(timer.handle, &due, 0, nullptr, nullptr, nullptr, 0) != FALSE) { + WaitForSingleObject(timer.handle, INFINITE); + return; + } + } +#endif + std::this_thread::sleep_for(std::chrono::microseconds(micros)); +} + +#endif diff --git a/core/libs/prx/libc/src/FormattingWide.cpp b/core/libs/prx/libc/src/FormattingWide.cpp new file mode 100644 index 000000000..5c44a994e --- /dev/null +++ b/core/libs/prx/libc/src/FormattingWide.cpp @@ -0,0 +1,224 @@ +// Wide-character (UTF-16 guest wchar_t) printf-family support. Upstream's Formatting.cpp implements the +// narrow checked/unchecked printf and scanf families but has no real wide-char formatter; vswprintf and +// wprintf are otherwise left as unimplemented stubs. +#include +#include +#include +#include +#include +#include +#include +#include + +#include "SceTypes.hpp" +#include "prx/libc/include/General.hpp" +#include "prx/libc/include/VarArgsAbi.hpp" +#include "prx/libc/include/WindowsFormatting.hpp" + +#ifdef _WIN32 +#define APS5_VA_BEGIN(last) __builtin_sysv_va_list args; __builtin_sysv_va_start(args, last) +#define APS5_VA_END() __builtin_sysv_va_end(args) +#else +#define APS5_VA_BEGIN(last) std::va_list args; va_start(args, last) +#define APS5_VA_END() va_end(args) +#endif + +namespace { + +bool In(char16_t character, const char* set) { + return character != 0 && character < 128 && std::strchr(set, static_cast(character)) != nullptr; +} + +void AppendUtf16(std::u16string& out, const char* text, std::size_t limit) { + for (std::size_t index = 0; index < limit && text[index] != '\0';) { + const unsigned char lead = static_cast(text[index]); + std::size_t extra = lead >= 0xf0 ? 3 : lead >= 0xe0 ? 2 : lead >= 0xc0 ? 1 : 0; + std::uint32_t code = extra == 3 ? lead & 0x07 : extra == 2 ? lead & 0x0f : extra == 1 ? lead & 0x1f : lead; + std::size_t used = 1; + for (; used <= extra; ++used) { + const unsigned char next = static_cast(text[index + used]); + if ((next & 0xc0) != 0x80) { extra = 0; code = lead; used = 1; break; } + code = (code << 6) | (next & 0x3f); + } + if (extra != 0) used = extra + 1; + index += used; + if (code >= 0x10000) { + code -= 0x10000; + out.push_back(static_cast(0xd800 + (code >> 10))); + out.push_back(static_cast(0xdc00 + (code & 0x3ff))); + } else out.push_back(static_cast(code)); + } +} + +std::string ToUtf8(const std::u16string& text) { + std::string out; + for (std::size_t index = 0; index < text.size(); ++index) { + std::uint32_t code = text[index]; + if (code >= 0xd800 && code < 0xdc00 && index + 1 < text.size() && text[index + 1] >= 0xdc00 && text[index + 1] < 0xe000) + code = 0x10000 + ((code - 0xd800) << 10) + (text[++index] - 0xdc00); + if (code < 0x80) out.push_back(static_cast(code)); + else if (code < 0x800) { + out.push_back(static_cast(0xc0 | (code >> 6))); + out.push_back(static_cast(0x80 | (code & 0x3f))); + } else if (code < 0x10000) { + out.push_back(static_cast(0xe0 | (code >> 12))); + out.push_back(static_cast(0x80 | ((code >> 6) & 0x3f))); + out.push_back(static_cast(0x80 | (code & 0x3f))); + } else { + out.push_back(static_cast(0xf0 | (code >> 18))); + out.push_back(static_cast(0x80 | ((code >> 12) & 0x3f))); + out.push_back(static_cast(0x80 | ((code >> 6) & 0x3f))); + out.push_back(static_cast(0x80 | (code & 0x3f))); + } + } + return out; +} + +template void AppendNumber(std::u16string& out, const std::string& spec, T value) { + const int size = std::snprintf(nullptr, 0, spec.c_str(), value); + if (size < 0) throw std::runtime_error("Formatting conversion failed"); + std::string text(static_cast(size) + 1, '\0'); + std::snprintf(&text[0], text.size(), spec.c_str(), value); + out.append(text.begin(), text.begin() + size); +} + +void AppendPadded(std::u16string& out, const std::u16string& text, bool left, int width) { + const std::size_t pad = width > 0 && static_cast(width) > text.size() ? static_cast(width) - text.size() : 0; + if (!left) out.append(pad, u' '); + out += text; + if (left) out.append(pad, u' '); +} + +// Wide printf: %s is a narrow string and %ls a wide one; numeric conversions reuse the host formatter. +std::u16string FormatWide(const char16_t* format, VaList* source) { + if (format == nullptr || source == nullptr) throw std::invalid_argument("Null formatting argument"); + LibcDetail::FormatArguments args(source); + std::u16string out; + while (*format) { + if (*format != u'%') { out.push_back(*format++); continue; } + ++format; + if (*format == u'%') { out.push_back(*format++); continue; } + std::string spec = "%"; + bool left = false; + int width = 0; + int precision = -1; + while (In(*format, "-+ #0")) { + if (*format == u'-') left = true; + spec += static_cast(*format++); + } + if (*format == u'*') { + ++format; + width = args.Next(); + if (width < 0) { left = true; spec += '-'; width = -width; } + spec += std::to_string(width); + } else { + while (*format >= u'0' && *format <= u'9') { width = width * 10 + (*format - u'0'); spec += static_cast(*format++); } + } + if (*format == u'.') { + ++format; + precision = 0; + if (*format == u'*') { + ++format; + precision = args.Next(); + if (precision >= 0) spec += "." + std::to_string(precision); + } else { + spec += '.'; + while (*format >= u'0' && *format <= u'9') { precision = precision * 10 + (*format - u'0'); spec += static_cast(*format++); } + } + } + std::string length; + if (In(*format, "hljztL")) { + length += static_cast(*format++); + if ((length == "h" && *format == u'h') || (length == "l" && *format == u'l')) length += static_cast(*format++); + } + const char16_t conversion = *format; + if (!conversion) throw std::invalid_argument("Incomplete format conversion"); + ++format; + const bool integerLength = length.empty() || length == "h" || length == "hh" || length == "l" || length == "ll" || + length == "j" || length == "z" || length == "t"; + if (conversion == u'd' || conversion == u'i') { + if (!integerLength) throw std::invalid_argument("Invalid integer length"); + long long value; + if (length.empty() || length == "h" || length == "hh") { + value = args.Next(); + if (length == "h") value = static_cast(value); + if (length == "hh") value = static_cast(value); + } else value = args.Next(); + AppendNumber(out, spec + "ll" + static_cast(conversion), value); + } else if (In(conversion, "ouxX")) { + if (!integerLength) throw std::invalid_argument("Invalid integer length"); + unsigned long long value; + if (length.empty() || length == "h" || length == "hh") { + value = args.Next(); + if (length == "h") value = static_cast(value); + if (length == "hh") value = static_cast(value); + } else value = args.Next(); + AppendNumber(out, spec + "ll" + static_cast(conversion), value); + } else if (In(conversion, "aAeEfFgG")) { + if (length == "L") AppendNumber(out, spec + "L" + static_cast(conversion), args.Next()); + else { + if (!length.empty() && length != "l") throw std::invalid_argument("Invalid floating length"); + AppendNumber(out, spec + static_cast(conversion), args.Next()); + } + } else if (conversion == u'p' && length.empty()) { + AppendNumber(out, spec + 'p', args.Next()); + } else if (conversion == u'c' || conversion == u'C') { + const int value = args.Next(); + std::u16string text; + if (conversion == u'C' || length == "l") text.push_back(static_cast(value)); + else text.push_back(static_cast(static_cast(value))); + AppendPadded(out, text, left, width); + } else if (conversion == u's' || conversion == u'S') { + std::u16string text; + const std::size_t limit = precision < 0 ? SIZE_MAX : static_cast(precision); + if (conversion == u'S' || length == "l") { + const char16_t* value = args.Next(); + if (value == nullptr) value = u"(null)"; + for (std::size_t index = 0; index < limit && value[index] != 0; ++index) text.push_back(value[index]); + } else { + const char* value = args.Next(); + AppendUtf16(text, value != nullptr ? value : "(null)", limit); + } + AppendPadded(out, text, left, width); + } else { + throw std::invalid_argument("Unsupported format conversion"); + } + } + return out; +} + +} // namespace + +extern "C" { + +// Returns the character count, or -1 when the buffer is too small (the output is still terminated). +int APS5_VABI vswprintf_nid_postfix(char16_t* buffer, std::size_t size, const char16_t* format, VaList* args) { + if (buffer == nullptr || size == 0) { errno = 22; return -1; } + try { + const std::u16string text = FormatWide(format, args); + const std::size_t copied = text.size() < size - 1 ? text.size() : size - 1; + std::memcpy(buffer, text.data(), copied * sizeof(char16_t)); + buffer[copied] = 0; + return copied == text.size() ? static_cast(copied) : -1; + } catch (const std::exception&) { + buffer[0] = 0; + errno = 22; + return -1; + } +} + +int APS5_VABI wprintf_nid_postfix(const char16_t* format, ...) { + APS5_VA_BEGIN(format); + int result = -1; + try { + const std::u16string text = FormatWide(format, reinterpret_cast(args)); + const std::string bytes = ToUtf8(text); + if (std::fwrite(bytes.data(), 1, bytes.size(), stdout) == bytes.size()) result = static_cast(text.size()); + } catch (const std::exception&) { + errno = 22; + } + APS5_VA_END(); + return result; +} + +} // extern "C" From cfe9a458cc75e8f4c01ba79447860d327f9b4dc8 Mon Sep 17 00:00:00 2001 From: OverkillLabs <10800190+OverkillLabs@users.noreply.github.com> Date: Mon, 28 Sep 2026 13:18:25 +0300 Subject: [PATCH 2/4] kernel: direct-memory pool querying and module loader wiring - DirectMemory: track each allocated block's type and extent in the physical pool (DirectMemoryBlock), splitting the record on a partial free, and expose DirectMemoryQueryBlock/DirectMemoryFreeRun. Wires sceKernelDirectMemoryQuery to answer with the real allocated range and type instead of a hardcoded one-page/type-3 guess, and sceKernelAvailableDirectMemorySize to report the actual free run instead of the whole remaining aperture. DirectMemory.cpp's mapping path already tolerates MAP_FIXED overlap into a registered range and already has honor-then-fallback address-hint handling and an ignored-flags mask, so it is unchanged here. - MemoryPoolExport: implement sceKernelMemoryPoolBatch/Commit/Decommit/ Expand/GetBlockStats/Reserve (previously all dead stubs) using the existing DoMprotect/DirectMemoryAlloc/DoReserveVirtual primitives and a small pool-state block counter. - Module: wire sceKernelDlsym/sceKernelLoadStartModule/ sceKernelStopUnloadModule to the existing host-module loader (dlopen_nid_postfix/dlsym_nid_postfix/dlclose_nid_postfix), replacing dead stubs. sceKernelGetModuleInfoForUnwind/FromAddr are unrelated and left as-is. --- .../libkernel/DirectMemory/DirectMemory.hpp | 18 ++- .../prx/libkernel/DirectMemory/Export.cpp | 24 +++- .../prx/libkernel/DirectMemory/MemoryPool.cpp | 51 ++++++- .../prx/libkernel/DirectMemory/MemoryPool.hpp | 6 +- .../DirectMemory/MemoryPoolExport.cpp | 132 ++++++++++++++---- core/libs/prx/libkernel/Module/src/Module.cpp | 32 +++-- 6 files changed, 206 insertions(+), 57 deletions(-) diff --git a/core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp b/core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp index ff97aaeb1..b98200f15 100644 --- a/core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp +++ b/core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp @@ -13,8 +13,24 @@ static constexpr int SCE_KERNEL_ERROR_ENOMEM = 0x8002000C; static constexpr int SCE_KERNEL_ERROR_EACCES = 0x8002000D; static constexpr int SCE_KERNEL_ERROR_EFAULT = 0x8002000E; -int DirectMemoryAlloc(int64_t searchStart, int64_t searchEnd, size_t len, size_t alignment, int64_t* physOut); +// A block of GPU-visible ("direct") memory the guest asked for, remembered so that later +// queries can answer with the real extent and the type it was allocated as. A title that gets +// back a one-page extent or a type it never requested rejects its own graphics allocations. +struct DirectMemoryBlock { + uint64_t start; + uint64_t end; + int memoryType; +}; + +// memoryType is what sceKernelAllocateDirectMemory was called with; -1 for a range the caller +// has no type for (the pool API and the available-size probe). +int DirectMemoryAlloc(int64_t searchStart, int64_t searchEnd, size_t len, size_t alignment, int memoryType, int64_t* physOut); void DirectMemoryFree(int64_t start, size_t len); +// Block containing offset, as the guest was given it at allocation time. +bool DirectMemoryQueryBlock(uint64_t offset, DirectMemoryBlock* block); +// Contiguous unallocated bytes starting at offset, capped at limit (what the available-size +// query reports; a whole-aperture figure would let a title plan allocations that cannot fit). +size_t DirectMemoryFreeRun(uint64_t offset, uint64_t limit); int DoMapDirect(void** addr, size_t len, int prot, int flags, int64_t physStart, size_t alignment); int DoMapAnon(void** addr, size_t len, int prot, int flags); int DoMprotect(const void* addr, size_t len, int prot); diff --git a/core/libs/prx/libkernel/DirectMemory/Export.cpp b/core/libs/prx/libkernel/DirectMemory/Export.cpp index 9a9d4eb6a..7d6ec6255 100644 --- a/core/libs/prx/libkernel/DirectMemory/Export.cpp +++ b/core/libs/prx/libkernel/DirectMemory/Export.cpp @@ -111,12 +111,11 @@ void _releaseFlexible(uintptr_t start, size_t len) { extern "C" { int APS5_VABI sceKernelAllocateDirectMemory(int64_t search_start, int64_t search_end, size_t len, size_t alignment, int memory_type, int64_t* phys_addr_out) { - (void)memory_type; if (search_start < 0 || search_end <= search_start || len == 0 || (len & (PS5_PAGE_SIZE - 1)) || !phys_addr_out || (alignment != 0 && (alignment & (PS5_PAGE_SIZE - 1)))) return SCE_KERNEL_ERROR_EINVAL; - return DirectMemoryAlloc(search_start, search_end, len, alignment, phys_addr_out); + return DirectMemoryAlloc(search_start, search_end, len, alignment, memory_type, phys_addr_out); } int APS5_VABI sceKernelAllocateMainDirectMemory(size_t len, size_t alignment, int memory_type, int64_t* phys_addr_out) { @@ -126,11 +125,13 @@ int APS5_VABI sceKernelAllocateMainDirectMemory(size_t len, size_t alignment, in int APS5_VABI sceKernelAvailableDirectMemorySize(int64_t search_start, int64_t search_end, size_t alignment, int64_t* phys_addr_out, size_t* size_out) { if (!phys_addr_out || !size_out) return SCE_KERNEL_ERROR_EINVAL; int64_t tmpPhys = 0; - int ret = DirectMemoryAlloc(search_start, search_end, PS5_PAGE_SIZE, alignment, &tmpPhys); + int ret = DirectMemoryAlloc(search_start, search_end, PS5_PAGE_SIZE, alignment, -1, &tmpPhys); if (ret != 0) { *phys_addr_out = 0; *size_out = 0; return ret; } DirectMemoryFree(tmpPhys, PS5_PAGE_SIZE); *phys_addr_out = tmpPhys; - *size_out = static_cast(search_end) - static_cast(tmpPhys); + // The probe only proves one page is free; report the run actually available from there so a + // title cannot plan an allocation that immediately fails. + *size_out = DirectMemoryFreeRun(static_cast(tmpPhys), static_cast(search_end)); return 0; } @@ -140,9 +141,18 @@ int APS5_VABI sceKernelDirectMemoryQuery(int64_t offset, int flags, void* info, struct DirectMemoryQueryInfo { int64_t start; int64_t end; int memory_type; }; if (info_size < sizeof(DirectMemoryQueryInfo)) return SCE_KERNEL_ERROR_EINVAL; auto* q = static_cast(info); - q->start = offset & ~static_cast(PS5_PAGE_SIZE - 1); - q->end = q->start + PS5_PAGE_SIZE; - q->memory_type = 3; + DirectMemoryBlock block{}; + if (DirectMemoryQueryBlock(static_cast(offset), &block)) { + q->start = static_cast(block.start); + q->end = static_cast(block.end); + q->memory_type = block.memoryType; + } else { + // Unallocated range: report the containing page as free-form (-1), same as an unregistered + // range has no type to answer with. + q->start = offset & ~static_cast(PS5_PAGE_SIZE - 1); + q->end = q->start + PS5_PAGE_SIZE; + q->memory_type = -1; + } return 0; } diff --git a/core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp b/core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp index 0560d0fbd..edad31693 100644 --- a/core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp +++ b/core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp @@ -1,6 +1,7 @@ #define _GLIBCXX_HAS_GTHREADS 0 #include "MemoryPool.hpp" #include "DirectMemory.hpp" +#include #include static constexpr size_t NUM_PAGES = DIRECT_MEMORY_SIZE / PS5_PAGE_SIZE; @@ -11,7 +12,7 @@ struct PhysicalMemoryPool { return inst; } - int Alloc(int64_t searchStart, int64_t searchEnd, size_t len, size_t alignment, int64_t* physOut) { + int Alloc(int64_t searchStart, int64_t searchEnd, size_t len, size_t alignment, int memoryType, int64_t* physOut) { std::lock_guard lock(_mutex); size_t align = (alignment == 0) ? PS5_PAGE_SIZE : alignment; uint64_t start = static_cast(searchStart); @@ -20,6 +21,7 @@ struct PhysicalMemoryPool { while (cur + len <= end && cur + len <= DIRECT_MEMORY_SIZE) { if (_isFree(cur, len)) { _mark(cur, len, true); + _ranges[cur] = {cur, cur + len, memoryType}; *physOut = static_cast(cur); return 0; } @@ -31,6 +33,40 @@ struct PhysicalMemoryPool { void Free(uint64_t start, size_t len) { std::lock_guard lock(_mutex); _mark(start, len, false); + // A partial release leaves the rest of the block queryable, so the record is split + // rather than dropped. + auto it = _ranges.upper_bound(start); + if (it == _ranges.begin()) return; + --it; + if (start + len > it->second.end) return; + const DirectMemoryBlock block = it->second; + _ranges.erase(it); + if (block.start < start) _ranges[block.start] = {block.start, start, block.memoryType}; + if (start + len < block.end) _ranges[start + len] = {start + len, block.end, block.memoryType}; + } + + // Block containing offset, as the guest was given it at allocation time. + bool Query(uint64_t offset, DirectMemoryBlock* block) { + std::lock_guard lock(_mutex); + auto it = _ranges.upper_bound(offset); + if (it == _ranges.begin()) return false; + --it; + if (offset >= it->second.end) return false; + *block = it->second; + return true; + } + + // Contiguous free bytes from `offset` up to `limit`. + size_t FreeRun(uint64_t offset, uint64_t limit) { + std::lock_guard lock(_mutex); + uint64_t cur = offset & ~static_cast(PS5_PAGE_SIZE - 1); + size_t run = 0; + while (cur + PS5_PAGE_SIZE <= limit && cur + PS5_PAGE_SIZE <= DIRECT_MEMORY_SIZE + && _isFree(cur, PS5_PAGE_SIZE)) { + cur += PS5_PAGE_SIZE; + run += PS5_PAGE_SIZE; + } + return run; } private: @@ -51,12 +87,21 @@ private: std::mutex _mutex; bool _used[NUM_PAGES] = {}; + std::map _ranges; }; -int DirectMemoryAlloc(int64_t searchStart, int64_t searchEnd, size_t len, size_t alignment, int64_t* physOut) { - return PhysicalMemoryPool::Instance().Alloc(searchStart, searchEnd, len, alignment, physOut); +int DirectMemoryAlloc(int64_t searchStart, int64_t searchEnd, size_t len, size_t alignment, int memoryType, int64_t* physOut) { + return PhysicalMemoryPool::Instance().Alloc(searchStart, searchEnd, len, alignment, memoryType, physOut); } void DirectMemoryFree(int64_t start, size_t len) { PhysicalMemoryPool::Instance().Free(static_cast(start), len); } + +bool DirectMemoryQueryBlock(uint64_t offset, DirectMemoryBlock* block) { + return PhysicalMemoryPool::Instance().Query(offset, block); +} + +size_t DirectMemoryFreeRun(uint64_t offset, uint64_t limit) { + return PhysicalMemoryPool::Instance().FreeRun(offset, limit); +} diff --git a/core/libs/prx/libkernel/DirectMemory/MemoryPool.hpp b/core/libs/prx/libkernel/DirectMemory/MemoryPool.hpp index 86182dff7..6bf75c51b 100644 --- a/core/libs/prx/libkernel/DirectMemory/MemoryPool.hpp +++ b/core/libs/prx/libkernel/DirectMemory/MemoryPool.hpp @@ -1,10 +1,6 @@ #ifndef CORE_LIBS_PRX_LIBKERNEL_DIRECTMEMORY_MEMORYPOOL_HPP #define CORE_LIBS_PRX_LINKERNEL_DIRECTMEMORY_MEMORYPOOL_HPP -#include -#include - -int DirectMemoryAlloc(int64_t searchStart, int64_t searchEnd, size_t len, size_t alignment, int64_t* physOut); -void DirectMemoryFree(int64_t start, size_t len); +#include "DirectMemory.hpp" #endif diff --git a/core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp b/core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp index 8922abc29..fa0921a76 100644 --- a/core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp +++ b/core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp @@ -1,62 +1,136 @@ #include #include +#include +#include #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" +#include "DirectMemory.hpp" + +namespace { + +// A pool block is the unit sceKernelMemoryPoolGetBlockStats counts. The pool itself is backed by +// ordinary direct-memory pages: Expand takes them, Reserve hands out an inaccessible virtual +// range, Commit makes part of it accessible and Decommit takes the access away again. +constexpr size_t kPoolBlockSize = 2ULL * 1024 * 1024; + +struct PoolState { + std::mutex mutex; + size_t expandedBytes = 0; + size_t committedBytes = 0; +}; + +PoolState& Pool() { + static PoolState* state = new PoolState(); + return *state; +} + +bool PageAligned(uint64_t v) { return (v & (PS5_PAGE_SIZE - 1)) == 0; } + +int PoolCommit(void* addr, uint64_t len, int prot) { + if (!addr || len == 0 || !PageAligned(reinterpret_cast(addr)) || !PageAligned(len)) return SCE_KERNEL_ERROR_EINVAL; + const int ret = DoMprotect(addr, static_cast(len), prot); + if (ret == 0) { + PoolState& pool = Pool(); + std::lock_guard lock(pool.mutex); + pool.committedBytes += static_cast(len); + } + return ret; +} + +int PoolDecommit(void* addr, uint64_t len) { + if (!addr || len == 0 || !PageAligned(reinterpret_cast(addr)) || !PageAligned(len)) return SCE_KERNEL_ERROR_EINVAL; + const int ret = DoMprotect(addr, static_cast(len), 0); + if (ret == 0) { + PoolState& pool = Pool(); + std::lock_guard lock(pool.mutex); + pool.committedBytes -= static_cast(len) < pool.committedBytes ? static_cast(len) : pool.committedBytes; + } + return ret; +} + +template +int Guarded(Fn fn) { + try { + return fn(); + } catch (const std::exception&) { + return SCE_KERNEL_ERROR_EINVAL; + } +} + +} // namespace extern "C" { +// Batch opcodes: 1 commit, 2 decommit, 3 protect, 4 type-protect. Move (5) is not supported. int APS5_VABI sceKernelMemoryPoolBatch(const KernelMemoryPoolBatchEntry* entries, int num_entries, int* num_entries_out, int flags) { - (void)entries; - (void)num_entries; - (void)num_entries_out; (void)flags; - NotImplemented_nid_no_patch(__func__); - return 0; + if (!entries || num_entries < 0) return SCE_KERNEL_ERROR_EINVAL; + int done = 0; + int ret = 0; + for (; done < num_entries; ++done) { + const KernelMemoryPoolBatchEntry& e = entries[done]; + ret = Guarded([&] { + switch (e.op) { + case 1: return PoolCommit(e.commit.addr, e.commit.len, e.commit.prot); + case 2: return PoolDecommit(e.decommit.addr, e.decommit.len); + case 3: return DoMprotect(e.protect.addr, static_cast(e.protect.len), e.protect.prot); + case 4: return DoMprotect(e.type_protect.addr, static_cast(e.type_protect.len), e.type_protect.prot); + default: return SCE_KERNEL_ERROR_EINVAL; + } + }); + if (ret != 0) break; + } + if (num_entries_out) *num_entries_out = done; + return ret; } int APS5_VABI sceKernelMemoryPoolCommit(void* addr, size_t len, int type, int prot, int flags) { - (void)addr; - (void)len; (void)type; - (void)prot; (void)flags; - NotImplemented_nid_no_patch(__func__); - return 0; + return Guarded([&] { return PoolCommit(addr, len, prot); }); } int APS5_VABI sceKernelMemoryPoolDecommit(void* addr, size_t len, int flags) { - (void)addr; - (void)len; (void)flags; - NotImplemented_nid_no_patch(__func__); - return 0; + return Guarded([&] { return PoolDecommit(addr, len); }); } int APS5_VABI sceKernelMemoryPoolExpand(int64_t search_start, int64_t search_end, size_t len, size_t alignment, int64_t* phys_addr_out) { - (void)search_start; - (void)search_end; - (void)len; - (void)alignment; - (void)phys_addr_out; - NotImplemented_nid_no_patch(__func__); - return 0; + if (search_start < 0 || search_end <= search_start || len == 0 || !PageAligned(len) || !phys_addr_out + || (alignment != 0 && !PageAligned(alignment))) { + return SCE_KERNEL_ERROR_EINVAL; + } + const int ret = DirectMemoryAlloc(search_start, search_end, len, alignment, -1, phys_addr_out); + if (ret == 0) { + PoolState& pool = Pool(); + std::lock_guard lock(pool.mutex); + pool.expandedBytes += len; + } + return ret; } +// Counts in whole pool blocks: expanded but uncommitted blocks are "available", committed ones "allocated". int APS5_VABI sceKernelMemoryPoolGetBlockStats(KernelMemoryPoolBlockStats* output, size_t output_size) { - (void)output; - (void)output_size; - NotImplemented_nid_no_patch(__func__); + if (!output || output_size < sizeof(KernelMemoryPoolBlockStats)) return SCE_KERNEL_ERROR_EINVAL; + PoolState& pool = Pool(); + std::lock_guard lock(pool.mutex); + const size_t total = (pool.expandedBytes + kPoolBlockSize - 1) / kPoolBlockSize; + const size_t used = (pool.committedBytes + kPoolBlockSize - 1) / kPoolBlockSize; + output->available_flushed_blocks = static_cast(total > used ? total - used : 0); + output->available_cached_blocks = 0; + output->allocated_flushed_blocks = static_cast(used); + output->allocated_cached_blocks = 0; return 0; } +// The reservation is an inaccessible virtual range; Commit later gives it access. int APS5_VABI sceKernelMemoryPoolReserve(void* addr_in, size_t len, size_t alignment, int flags, void** addr_out) { (void)addr_in; - (void)len; - (void)alignment; (void)flags; - (void)addr_out; - NotImplemented_nid_no_patch(__func__); - return 0; + if (!addr_out || len == 0 || !PageAligned(len) || (alignment != 0 && !PageAligned(alignment))) { + return SCE_KERNEL_ERROR_EINVAL; + } + return Guarded([&] { return DoReserveVirtual(addr_out, len, alignment); }); } } diff --git a/core/libs/prx/libkernel/Module/src/Module.cpp b/core/libs/prx/libkernel/Module/src/Module.cpp index 4b0f12136..10291d6e4 100644 --- a/core/libs/prx/libkernel/Module/src/Module.cpp +++ b/core/libs/prx/libkernel/Module/src/Module.cpp @@ -2,14 +2,23 @@ #include #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" +#include "prx/libkernel/DirectMemory/DirectMemory.hpp" + +extern "C" { +// DynamicLoader.cpp: host-module loader shared with dlopen/dlsym. +void* APS5_VABI dlopen_nid_postfix(const char* path, int flags); +void* APS5_VABI dlsym_nid_postfix(void* handle, const char* name); +int APS5_VABI dlclose_nid_postfix(void* handle); +} extern "C" { +// Module handles are the loader's dlopen handles, so only host-loadable modules resolve here. int APS5_VABI sceKernelDlsym(KernelModule handle, const char* symbol, void** addr) { - (void)handle; - (void)symbol; - (void)addr; - NotImplemented_nid_no_patch(__func__); + if (!symbol || !addr) return SCE_KERNEL_ERROR_EFAULT; + void* found = dlsym_nid_postfix(reinterpret_cast(static_cast(handle)), symbol); + if (!found) return static_cast(0x80020003); // ESRCH: symbol not found + *addr = found; return 0; } @@ -30,25 +39,24 @@ int APS5_VABI sceKernelGetModuleInfoFromAddr(uint64_t addr, int n, ModuleInfo* r } KernelModule APS5_VABI sceKernelLoadStartModule(const char* module_file_name, size_t args, const void* argp, uint32_t flags, const KernelLoadModuleOpt* opt, int* res) { - (void)module_file_name; (void)args; (void)argp; (void)flags; (void)opt; - (void)res; - NotImplemented_nid_no_patch(__func__); - return {}; + if (res) *res = 0; + if (!module_file_name) return static_cast(SCE_KERNEL_ERROR_EFAULT); + void* handle = dlopen_nid_postfix(module_file_name, 2); // RTLD_NOW + if (!handle) return static_cast(0x80020002); // ENOENT + return static_cast(reinterpret_cast(handle)); } int APS5_VABI sceKernelStopUnloadModule(KernelModule handle, size_t args, const void* argp, uint32_t flags, const KernelUnloadModuleOpt* opt, int* res) { - (void)handle; (void)args; (void)argp; (void)flags; (void)opt; - (void)res; - NotImplemented_nid_no_patch(__func__); - return 0; + if (res) *res = 0; + return dlclose_nid_postfix(reinterpret_cast(static_cast(handle))) == 0 ? 0 : static_cast(0x80020003); } } From d9f8d87c2724ad189eb07309b0664d7cd83df058 Mon Sep 17 00:00:00 2001 From: OverkillLabs <10800190+OverkillLabs@users.noreply.github.com> Date: Tue, 29 Sep 2026 04:18:04 +0300 Subject: [PATCH 3/4] refactor: strip explanatory comments to match repo conventions Comments in this codebase may only mark technical debt (per docs/dev/CONVENTIONS.md), plus the #endif and namespace-close structural exceptions. Removes the descriptive comments added on this branch across PreciseWait.hpp, FormattingWide.cpp, the direct-memory pool query/free-run plumbing, and the module loader wiring, keeping the two allowed structural markers intact. --- core/libs/prx/libc/Export.cpp | 2 -- core/libs/prx/libc/include/PreciseWait.hpp | 13 +------------ core/libs/prx/libc/src/FormattingWide.cpp | 5 ----- core/libs/prx/libkernel/DirectMemory/Export.cpp | 4 ---- core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp | 4 ---- .../prx/libkernel/DirectMemory/MemoryPoolExport.cpp | 6 ------ core/libs/prx/libkernel/Module/src/Module.cpp | 8 +++----- 7 files changed, 4 insertions(+), 38 deletions(-) diff --git a/core/libs/prx/libc/Export.cpp b/core/libs/prx/libc/Export.cpp index 84b1b05f4..be937cab9 100644 --- a/core/libs/prx/libc/Export.cpp +++ b/core/libs/prx/libc/Export.cpp @@ -81,8 +81,6 @@ int APS5_VABI fscanf_nid_postfix() { return 0; } -// vswprintf_nid_postfix is implemented in src/FormattingWide.cpp. - APS5_EXPORT("Pu0Ecyk-7FU", libcUnknown_Pu0Ecyk_M7FU); int APS5_VABI libcUnknown_Pu0Ecyk_M7FU() { NotImplemented_nid_no_patch(__func__); diff --git a/core/libs/prx/libc/include/PreciseWait.hpp b/core/libs/prx/libc/include/PreciseWait.hpp index fe1132248..06196d05f 100644 --- a/core/libs/prx/libc/include/PreciseWait.hpp +++ b/core/libs/prx/libc/include/PreciseWait.hpp @@ -8,17 +8,6 @@ #include #endif -// std::condition_variable::wait_for on this MinGW toolchain cannot return in under about one system tick, -// whatever the budget says: measured 15.6 ms average for a 0 us wait and for a 1 us wait alike, and raising -// the process timer resolution to 0.5 ms with NtSetTimerResolution does not change it. A high-resolution -// waitable timer honours the same request in about 0.5 ms instead. -// -// This matters for any guest-side poll that requests a sub-tick wait budget: without this helper, such a -// poll runs at the OS tick rate rather than at the requested budget, which can turn a tight wait loop into -// a multi-millisecond stall per iteration. -// -// Callers must hold no lock that the awaited event's producer needs: the point of the helper is to wait -// precisely and cheaply, so the caller re-tests its own condition afterwards. inline constexpr unsigned long long kSubTickWaitUs = 2000; inline void PreciseSleepUs(unsigned long long micros) { @@ -38,7 +27,7 @@ inline void PreciseSleepUs(unsigned long long micros) { thread_local Timer timer; if (timer.handle != nullptr) { LARGE_INTEGER due{}; - due.QuadPart = -static_cast(micros * 10ULL); // relative, 100 ns units + due.QuadPart = -static_cast(micros * 10ULL); if (SetWaitableTimerEx(timer.handle, &due, 0, nullptr, nullptr, nullptr, 0) != FALSE) { WaitForSingleObject(timer.handle, INFINITE); return; diff --git a/core/libs/prx/libc/src/FormattingWide.cpp b/core/libs/prx/libc/src/FormattingWide.cpp index 5c44a994e..7f886acc1 100644 --- a/core/libs/prx/libc/src/FormattingWide.cpp +++ b/core/libs/prx/libc/src/FormattingWide.cpp @@ -1,6 +1,3 @@ -// Wide-character (UTF-16 guest wchar_t) printf-family support. Upstream's Formatting.cpp implements the -// narrow checked/unchecked printf and scanf families but has no real wide-char formatter; vswprintf and -// wprintf are otherwise left as unimplemented stubs. #include #include #include @@ -89,7 +86,6 @@ void AppendPadded(std::u16string& out, const std::u16string& text, bool left, in if (left) out.append(pad, u' '); } -// Wide printf: %s is a narrow string and %ls a wide one; numeric conversions reuse the host formatter. std::u16string FormatWide(const char16_t* format, VaList* source) { if (format == nullptr || source == nullptr) throw std::invalid_argument("Null formatting argument"); LibcDetail::FormatArguments args(source); @@ -191,7 +187,6 @@ std::u16string FormatWide(const char16_t* format, VaList* source) { extern "C" { -// Returns the character count, or -1 when the buffer is too small (the output is still terminated). int APS5_VABI vswprintf_nid_postfix(char16_t* buffer, std::size_t size, const char16_t* format, VaList* args) { if (buffer == nullptr || size == 0) { errno = 22; return -1; } try { diff --git a/core/libs/prx/libkernel/DirectMemory/Export.cpp b/core/libs/prx/libkernel/DirectMemory/Export.cpp index 7d6ec6255..c7922f496 100644 --- a/core/libs/prx/libkernel/DirectMemory/Export.cpp +++ b/core/libs/prx/libkernel/DirectMemory/Export.cpp @@ -129,8 +129,6 @@ int APS5_VABI sceKernelAvailableDirectMemorySize(int64_t search_start, int64_t s if (ret != 0) { *phys_addr_out = 0; *size_out = 0; return ret; } DirectMemoryFree(tmpPhys, PS5_PAGE_SIZE); *phys_addr_out = tmpPhys; - // The probe only proves one page is free; report the run actually available from there so a - // title cannot plan an allocation that immediately fails. *size_out = DirectMemoryFreeRun(static_cast(tmpPhys), static_cast(search_end)); return 0; } @@ -147,8 +145,6 @@ int APS5_VABI sceKernelDirectMemoryQuery(int64_t offset, int flags, void* info, q->end = static_cast(block.end); q->memory_type = block.memoryType; } else { - // Unallocated range: report the containing page as free-form (-1), same as an unregistered - // range has no type to answer with. q->start = offset & ~static_cast(PS5_PAGE_SIZE - 1); q->end = q->start + PS5_PAGE_SIZE; q->memory_type = -1; diff --git a/core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp b/core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp index edad31693..5d4f7e01e 100644 --- a/core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp +++ b/core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp @@ -33,8 +33,6 @@ struct PhysicalMemoryPool { void Free(uint64_t start, size_t len) { std::lock_guard lock(_mutex); _mark(start, len, false); - // A partial release leaves the rest of the block queryable, so the record is split - // rather than dropped. auto it = _ranges.upper_bound(start); if (it == _ranges.begin()) return; --it; @@ -45,7 +43,6 @@ struct PhysicalMemoryPool { if (start + len < block.end) _ranges[start + len] = {start + len, block.end, block.memoryType}; } - // Block containing offset, as the guest was given it at allocation time. bool Query(uint64_t offset, DirectMemoryBlock* block) { std::lock_guard lock(_mutex); auto it = _ranges.upper_bound(offset); @@ -56,7 +53,6 @@ struct PhysicalMemoryPool { return true; } - // Contiguous free bytes from `offset` up to `limit`. size_t FreeRun(uint64_t offset, uint64_t limit) { std::lock_guard lock(_mutex); uint64_t cur = offset & ~static_cast(PS5_PAGE_SIZE - 1); diff --git a/core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp b/core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp index fa0921a76..108bf5076 100644 --- a/core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp +++ b/core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp @@ -8,9 +8,6 @@ namespace { -// A pool block is the unit sceKernelMemoryPoolGetBlockStats counts. The pool itself is backed by -// ordinary direct-memory pages: Expand takes them, Reserve hands out an inaccessible virtual -// range, Commit makes part of it accessible and Decommit takes the access away again. constexpr size_t kPoolBlockSize = 2ULL * 1024 * 1024; struct PoolState { @@ -61,7 +58,6 @@ int Guarded(Fn fn) { extern "C" { -// Batch opcodes: 1 commit, 2 decommit, 3 protect, 4 type-protect. Move (5) is not supported. int APS5_VABI sceKernelMemoryPoolBatch(const KernelMemoryPoolBatchEntry* entries, int num_entries, int* num_entries_out, int flags) { (void)flags; if (!entries || num_entries < 0) return SCE_KERNEL_ERROR_EINVAL; @@ -109,7 +105,6 @@ int APS5_VABI sceKernelMemoryPoolExpand(int64_t search_start, int64_t search_end return ret; } -// Counts in whole pool blocks: expanded but uncommitted blocks are "available", committed ones "allocated". int APS5_VABI sceKernelMemoryPoolGetBlockStats(KernelMemoryPoolBlockStats* output, size_t output_size) { if (!output || output_size < sizeof(KernelMemoryPoolBlockStats)) return SCE_KERNEL_ERROR_EINVAL; PoolState& pool = Pool(); @@ -123,7 +118,6 @@ int APS5_VABI sceKernelMemoryPoolGetBlockStats(KernelMemoryPoolBlockStats* outpu return 0; } -// The reservation is an inaccessible virtual range; Commit later gives it access. int APS5_VABI sceKernelMemoryPoolReserve(void* addr_in, size_t len, size_t alignment, int flags, void** addr_out) { (void)addr_in; (void)flags; diff --git a/core/libs/prx/libkernel/Module/src/Module.cpp b/core/libs/prx/libkernel/Module/src/Module.cpp index 10291d6e4..a4cc00605 100644 --- a/core/libs/prx/libkernel/Module/src/Module.cpp +++ b/core/libs/prx/libkernel/Module/src/Module.cpp @@ -5,7 +5,6 @@ #include "prx/libkernel/DirectMemory/DirectMemory.hpp" extern "C" { -// DynamicLoader.cpp: host-module loader shared with dlopen/dlsym. void* APS5_VABI dlopen_nid_postfix(const char* path, int flags); void* APS5_VABI dlsym_nid_postfix(void* handle, const char* name); int APS5_VABI dlclose_nid_postfix(void* handle); @@ -13,11 +12,10 @@ int APS5_VABI dlclose_nid_postfix(void* handle); extern "C" { -// Module handles are the loader's dlopen handles, so only host-loadable modules resolve here. int APS5_VABI sceKernelDlsym(KernelModule handle, const char* symbol, void** addr) { if (!symbol || !addr) return SCE_KERNEL_ERROR_EFAULT; void* found = dlsym_nid_postfix(reinterpret_cast(static_cast(handle)), symbol); - if (!found) return static_cast(0x80020003); // ESRCH: symbol not found + if (!found) return static_cast(0x80020003); *addr = found; return 0; } @@ -45,8 +43,8 @@ KernelModule APS5_VABI sceKernelLoadStartModule(const char* module_file_name, si (void)opt; if (res) *res = 0; if (!module_file_name) return static_cast(SCE_KERNEL_ERROR_EFAULT); - void* handle = dlopen_nid_postfix(module_file_name, 2); // RTLD_NOW - if (!handle) return static_cast(0x80020002); // ENOENT + void* handle = dlopen_nid_postfix(module_file_name, 2); + if (!handle) return static_cast(0x80020002); return static_cast(reinterpret_cast(handle)); } From 55ed2719d565786f3462a9b905f22d8832d57fb3 Mon Sep 17 00:00:00 2001 From: OverkillLabs <10800190+OverkillLabs@users.noreply.github.com> Date: Tue, 29 Sep 2026 04:20:28 +0300 Subject: [PATCH 4/4] refactor: apply naming conventions and drop extern-C comment Renames template parameters to the TKey-style the conventions doc requires (TValue, TFunction), removes the extern "C" close comment (not one of the two allowed structural exceptions), and replaces the magic error/flag numbers left after the previous comment cleanup with the existing SCE_KERNEL_ERROR_* constants and a named RTLD_NOW constant so the code documents itself. --- core/libs/prx/libc/src/FormattingWide.cpp | 4 ++-- .../prx/libkernel/DirectMemory/MemoryPoolExport.cpp | 6 +++--- core/libs/prx/libkernel/Module/src/Module.cpp | 13 +++++++++---- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/core/libs/prx/libc/src/FormattingWide.cpp b/core/libs/prx/libc/src/FormattingWide.cpp index 7f886acc1..bd97b0fbb 100644 --- a/core/libs/prx/libc/src/FormattingWide.cpp +++ b/core/libs/prx/libc/src/FormattingWide.cpp @@ -71,7 +71,7 @@ std::string ToUtf8(const std::u16string& text) { return out; } -template void AppendNumber(std::u16string& out, const std::string& spec, T value) { +template void AppendNumber(std::u16string& out, const std::string& spec, TValue value) { const int size = std::snprintf(nullptr, 0, spec.c_str(), value); if (size < 0) throw std::runtime_error("Formatting conversion failed"); std::string text(static_cast(size) + 1, '\0'); @@ -216,4 +216,4 @@ int APS5_VABI wprintf_nid_postfix(const char16_t* format, ...) { return result; } -} // extern "C" +} diff --git a/core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp b/core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp index 108bf5076..bf7315ce2 100644 --- a/core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp +++ b/core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp @@ -45,10 +45,10 @@ int PoolDecommit(void* addr, uint64_t len) { return ret; } -template -int Guarded(Fn fn) { +template +int Guarded(TFunction function) { try { - return fn(); + return function(); } catch (const std::exception&) { return SCE_KERNEL_ERROR_EINVAL; } diff --git a/core/libs/prx/libkernel/Module/src/Module.cpp b/core/libs/prx/libkernel/Module/src/Module.cpp index a4cc00605..951587400 100644 --- a/core/libs/prx/libkernel/Module/src/Module.cpp +++ b/core/libs/prx/libkernel/Module/src/Module.cpp @@ -3,6 +3,7 @@ #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" #include "prx/libkernel/DirectMemory/DirectMemory.hpp" +#include "prx/libkernel/KernelErrors.hpp" extern "C" { void* APS5_VABI dlopen_nid_postfix(const char* path, int flags); @@ -10,12 +11,16 @@ void* APS5_VABI dlsym_nid_postfix(void* handle, const char* name); int APS5_VABI dlclose_nid_postfix(void* handle); } +namespace { +constexpr int kRtldNow = 2; +} + extern "C" { int APS5_VABI sceKernelDlsym(KernelModule handle, const char* symbol, void** addr) { if (!symbol || !addr) return SCE_KERNEL_ERROR_EFAULT; void* found = dlsym_nid_postfix(reinterpret_cast(static_cast(handle)), symbol); - if (!found) return static_cast(0x80020003); + if (!found) return SCE_KERNEL_ERROR_ESRCH; *addr = found; return 0; } @@ -43,8 +48,8 @@ KernelModule APS5_VABI sceKernelLoadStartModule(const char* module_file_name, si (void)opt; if (res) *res = 0; if (!module_file_name) return static_cast(SCE_KERNEL_ERROR_EFAULT); - void* handle = dlopen_nid_postfix(module_file_name, 2); - if (!handle) return static_cast(0x80020002); + void* handle = dlopen_nid_postfix(module_file_name, kRtldNow); + if (!handle) return static_cast(SCE_KERNEL_ERROR_ENOENT); return static_cast(reinterpret_cast(handle)); } @@ -54,7 +59,7 @@ int APS5_VABI sceKernelStopUnloadModule(KernelModule handle, size_t args, const (void)flags; (void)opt; if (res) *res = 0; - return dlclose_nid_postfix(reinterpret_cast(static_cast(handle))) == 0 ? 0 : static_cast(0x80020003); + return dlclose_nid_postfix(reinterpret_cast(static_cast(handle))) == 0 ? 0 : SCE_KERNEL_ERROR_ESRCH; } }