diff --git a/core/libs/prx/libc/CMakeLists.txt b/core/libs/prx/libc/CMakeLists.txt index 88e49d7ac..d8e52f150 100644 --- a/core/libs/prx/libc/CMakeLists.txt +++ b/core/libs/prx/libc/CMakeLists.txt @@ -22,6 +22,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..be937cab9 100644 --- a/core/libs/prx/libc/Export.cpp +++ b/core/libs/prx/libc/Export.cpp @@ -81,11 +81,6 @@ int APS5_VABI fscanf_nid_postfix() { return 0; } -int APS5_VABI vswprintf_nid_postfix() { - NotImplemented_nid_no_patch(__func__); - return 0; -} - 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 new file mode 100644 index 000000000..06196d05f --- /dev/null +++ b/core/libs/prx/libc/include/PreciseWait.hpp @@ -0,0 +1,40 @@ +#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_PRECISEWAIT_HPP +#define CORE_LIBS_PRX_LIBC_INCLUDE_PRECISEWAIT_HPP + +#include +#include + +#ifdef _WIN32 +#include +#endif + +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); + 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..bd97b0fbb --- /dev/null +++ b/core/libs/prx/libc/src/FormattingWide.cpp @@ -0,0 +1,219 @@ +#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, 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'); + 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' '); +} + +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" { + +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; +} + +} diff --git a/core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp b/core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp index 4c7f3f477..53289beff 100644 --- a/core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp +++ b/core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp @@ -9,8 +9,16 @@ static constexpr size_t DIRECT_MEMORY_SIZE = 13824ULL * 1024 * 1024; static constexpr size_t PS5_PAGE_SIZE = 0x4000; -int DirectMemoryAlloc(int64_t searchStart, int64_t searchEnd, size_t len, size_t alignment, int64_t* physOut); +struct DirectMemoryBlock { + uint64_t start; + uint64_t end; + int memoryType; +}; + +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); +bool DirectMemoryQueryBlock(uint64_t offset, DirectMemoryBlock* block); +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..c7922f496 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,11 @@ 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); + *size_out = DirectMemoryFreeRun(static_cast(tmpPhys), static_cast(search_end)); return 0; } @@ -140,9 +139,16 @@ 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 { + 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..5d4f7e01e 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,36 @@ struct PhysicalMemoryPool { void Free(uint64_t start, size_t len) { std::lock_guard lock(_mutex); _mark(start, len, false); + 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}; + } + + 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; + } + + 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 +83,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..bf7315ce2 100644 --- a/core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp +++ b/core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp @@ -1,62 +1,130 @@ #include #include +#include +#include #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" +#include "DirectMemory.hpp" + +namespace { + +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(TFunction function) { + try { + return function(); + } catch (const std::exception&) { + return SCE_KERNEL_ERROR_EINVAL; + } +} + +} // namespace extern "C" { 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; } 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; } 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..951587400 100644 --- a/core/libs/prx/libkernel/Module/src/Module.cpp +++ b/core/libs/prx/libkernel/Module/src/Module.cpp @@ -2,14 +2,26 @@ #include #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); +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) { - (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 SCE_KERNEL_ERROR_ESRCH; + *addr = found; return 0; } @@ -30,25 +42,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, kRtldNow); + if (!handle) return static_cast(SCE_KERNEL_ERROR_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 : SCE_KERNEL_ERROR_ESRCH; } }