mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #81 from OverkillLabs/feat/kernel-libc-fixes
feat(libc,kernel): wide-char formatting, sub-tick waits, memory-pool querying, module loader wiring
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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__);
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_PRECISEWAIT_HPP
|
||||
#define CORE_LIBS_PRX_LIBC_INCLUDE_PRECISEWAIT_HPP
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#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<LONGLONG>(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
|
||||
@@ -0,0 +1,219 @@
|
||||
#include <cerrno>
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <cstdarg>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#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<char>(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<unsigned char>(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<unsigned char>(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<char16_t>(0xd800 + (code >> 10)));
|
||||
out.push_back(static_cast<char16_t>(0xdc00 + (code & 0x3ff)));
|
||||
} else out.push_back(static_cast<char16_t>(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<char>(code));
|
||||
else if (code < 0x800) {
|
||||
out.push_back(static_cast<char>(0xc0 | (code >> 6)));
|
||||
out.push_back(static_cast<char>(0x80 | (code & 0x3f)));
|
||||
} else if (code < 0x10000) {
|
||||
out.push_back(static_cast<char>(0xe0 | (code >> 12)));
|
||||
out.push_back(static_cast<char>(0x80 | ((code >> 6) & 0x3f)));
|
||||
out.push_back(static_cast<char>(0x80 | (code & 0x3f)));
|
||||
} else {
|
||||
out.push_back(static_cast<char>(0xf0 | (code >> 18)));
|
||||
out.push_back(static_cast<char>(0x80 | ((code >> 12) & 0x3f)));
|
||||
out.push_back(static_cast<char>(0x80 | ((code >> 6) & 0x3f)));
|
||||
out.push_back(static_cast<char>(0x80 | (code & 0x3f)));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
template<class TValue> 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<std::size_t>(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<std::size_t>(width) > text.size() ? static_cast<std::size_t>(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<char>(*format++);
|
||||
}
|
||||
if (*format == u'*') {
|
||||
++format;
|
||||
width = args.Next<int>();
|
||||
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<char>(*format++); }
|
||||
}
|
||||
if (*format == u'.') {
|
||||
++format;
|
||||
precision = 0;
|
||||
if (*format == u'*') {
|
||||
++format;
|
||||
precision = args.Next<int>();
|
||||
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<char>(*format++); }
|
||||
}
|
||||
}
|
||||
std::string length;
|
||||
if (In(*format, "hljztL")) {
|
||||
length += static_cast<char>(*format++);
|
||||
if ((length == "h" && *format == u'h') || (length == "l" && *format == u'l')) length += static_cast<char>(*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<int>();
|
||||
if (length == "h") value = static_cast<short>(value);
|
||||
if (length == "hh") value = static_cast<signed char>(value);
|
||||
} else value = args.Next<long long>();
|
||||
AppendNumber(out, spec + "ll" + static_cast<char>(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<unsigned int>();
|
||||
if (length == "h") value = static_cast<unsigned short>(value);
|
||||
if (length == "hh") value = static_cast<unsigned char>(value);
|
||||
} else value = args.Next<unsigned long long>();
|
||||
AppendNumber(out, spec + "ll" + static_cast<char>(conversion), value);
|
||||
} else if (In(conversion, "aAeEfFgG")) {
|
||||
if (length == "L") AppendNumber(out, spec + "L" + static_cast<char>(conversion), args.Next<long double>());
|
||||
else {
|
||||
if (!length.empty() && length != "l") throw std::invalid_argument("Invalid floating length");
|
||||
AppendNumber(out, spec + static_cast<char>(conversion), args.Next<double>());
|
||||
}
|
||||
} else if (conversion == u'p' && length.empty()) {
|
||||
AppendNumber(out, spec + 'p', args.Next<void*>());
|
||||
} else if (conversion == u'c' || conversion == u'C') {
|
||||
const int value = args.Next<int>();
|
||||
std::u16string text;
|
||||
if (conversion == u'C' || length == "l") text.push_back(static_cast<char16_t>(value));
|
||||
else text.push_back(static_cast<char16_t>(static_cast<unsigned char>(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<std::size_t>(precision);
|
||||
if (conversion == u'S' || length == "l") {
|
||||
const char16_t* value = args.Next<const char16_t*>();
|
||||
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<const char*>();
|
||||
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<int>(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<VaList*>(args));
|
||||
const std::string bytes = ToUtf8(text);
|
||||
if (std::fwrite(bytes.data(), 1, bytes.size(), stdout) == bytes.size()) result = static_cast<int>(text.size());
|
||||
} catch (const std::exception&) {
|
||||
errno = 22;
|
||||
}
|
||||
APS5_VA_END();
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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<size_t>(search_end) - static_cast<size_t>(tmpPhys);
|
||||
*size_out = DirectMemoryFreeRun(static_cast<uint64_t>(tmpPhys), static_cast<uint64_t>(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<DirectMemoryQueryInfo*>(info);
|
||||
q->start = offset & ~static_cast<int64_t>(PS5_PAGE_SIZE - 1);
|
||||
q->end = q->start + PS5_PAGE_SIZE;
|
||||
q->memory_type = 3;
|
||||
DirectMemoryBlock block{};
|
||||
if (DirectMemoryQueryBlock(static_cast<uint64_t>(offset), &block)) {
|
||||
q->start = static_cast<int64_t>(block.start);
|
||||
q->end = static_cast<int64_t>(block.end);
|
||||
q->memory_type = block.memoryType;
|
||||
} else {
|
||||
q->start = offset & ~static_cast<int64_t>(PS5_PAGE_SIZE - 1);
|
||||
q->end = q->start + PS5_PAGE_SIZE;
|
||||
q->memory_type = -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#define _GLIBCXX_HAS_GTHREADS 0
|
||||
#include "MemoryPool.hpp"
|
||||
#include "DirectMemory.hpp"
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
|
||||
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<std::mutex> lock(_mutex);
|
||||
size_t align = (alignment == 0) ? PS5_PAGE_SIZE : alignment;
|
||||
uint64_t start = static_cast<uint64_t>(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<int64_t>(cur);
|
||||
return 0;
|
||||
}
|
||||
@@ -31,6 +33,36 @@ struct PhysicalMemoryPool {
|
||||
void Free(uint64_t start, size_t len) {
|
||||
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> lock(_mutex);
|
||||
uint64_t cur = offset & ~static_cast<uint64_t>(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<uint64_t, DirectMemoryBlock> _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<uint64_t>(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);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
#ifndef CORE_LIBS_PRX_LIBKERNEL_DIRECTMEMORY_MEMORYPOOL_HPP
|
||||
#define CORE_LIBS_PRX_LINKERNEL_DIRECTMEMORY_MEMORYPOOL_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
|
||||
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
|
||||
|
||||
@@ -1,62 +1,130 @@
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <mutex>
|
||||
#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<uintptr_t>(addr)) || !PageAligned(len)) return SCE_KERNEL_ERROR_EINVAL;
|
||||
const int ret = DoMprotect(addr, static_cast<size_t>(len), prot);
|
||||
if (ret == 0) {
|
||||
PoolState& pool = Pool();
|
||||
std::lock_guard<std::mutex> lock(pool.mutex);
|
||||
pool.committedBytes += static_cast<size_t>(len);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int PoolDecommit(void* addr, uint64_t len) {
|
||||
if (!addr || len == 0 || !PageAligned(reinterpret_cast<uintptr_t>(addr)) || !PageAligned(len)) return SCE_KERNEL_ERROR_EINVAL;
|
||||
const int ret = DoMprotect(addr, static_cast<size_t>(len), 0);
|
||||
if (ret == 0) {
|
||||
PoolState& pool = Pool();
|
||||
std::lock_guard<std::mutex> lock(pool.mutex);
|
||||
pool.committedBytes -= static_cast<size_t>(len) < pool.committedBytes ? static_cast<size_t>(len) : pool.committedBytes;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename TFunction>
|
||||
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<size_t>(e.protect.len), e.protect.prot);
|
||||
case 4: return DoMprotect(e.type_protect.addr, static_cast<size_t>(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<std::mutex> 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<std::mutex> 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<int32_t>(total > used ? total - used : 0);
|
||||
output->available_cached_blocks = 0;
|
||||
output->allocated_flushed_blocks = static_cast<int32_t>(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); });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,14 +2,26 @@
|
||||
#include <cstddef>
|
||||
#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<void*>(static_cast<intptr_t>(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<KernelModule>(SCE_KERNEL_ERROR_EFAULT);
|
||||
void* handle = dlopen_nid_postfix(module_file_name, kRtldNow);
|
||||
if (!handle) return static_cast<KernelModule>(SCE_KERNEL_ERROR_ENOENT);
|
||||
return static_cast<KernelModule>(reinterpret_cast<intptr_t>(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<void*>(static_cast<intptr_t>(handle))) == 0 ? 0 : SCE_KERNEL_ERROR_ESRCH;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user