mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(libkernel): libkernel direct memory impl
This commit is contained in:
@@ -4,7 +4,7 @@ Converter for native execution of PlayStation 5 ELF binaries on Linux through bi
|
||||
|
||||
## Status
|
||||
|
||||
Execution reaches `_start`, stack unwinding and exception handling tables are built. All unimplemented stub functions throw std::runtime_error. `what()` is printed to stderr and the process terminates. Now: `sceKernelGetDirectMemorySize not implemented`
|
||||
Execution reaches `_start`, stack unwinding and exception handling tables are built. All unimplemented stub functions throw std::runtime_error. `what()` is printed to stderr and the process terminates. Now: `pthread_mutex_lock not implemented`
|
||||
|
||||
## Disclaimer
|
||||
|
||||
|
||||
@@ -40,9 +40,16 @@ foreach(LIB_DIR ${LIBS_DIRS})
|
||||
if(IS_DIRECTORY ${LIB_DIR})
|
||||
get_filename_component(LIB_NAME ${LIB_DIR} NAME)
|
||||
|
||||
add_library(${LIB_NAME} SHARED EXCLUDE_FROM_ALL ${LIB_DIR}/Export.cpp)
|
||||
target_include_directories(${LIB_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
if(EXISTS ${LIB_DIR}/CMakeLists.txt)
|
||||
set(LIBS_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
add_subdirectory(${LIB_DIR} ${CMAKE_CURRENT_BINARY_DIR}/prx/${LIB_NAME})
|
||||
else()
|
||||
add_library(${LIB_NAME} SHARED EXCLUDE_FROM_ALL ${LIB_DIR}/Export.cpp)
|
||||
target_include_directories(${LIB_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
endif()
|
||||
|
||||
target_link_libraries(${LIB_NAME} PRIVATE libc)
|
||||
|
||||
set_target_properties(${LIB_NAME} PROPERTIES
|
||||
PREFIX ""
|
||||
SUFFIX .prx
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
add_library(libkernel SHARED EXCLUDE_FROM_ALL
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Export.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/MemoryPool.cpp
|
||||
)
|
||||
|
||||
target_include_directories(libkernel PRIVATE ${LIBS_INCLUDE_DIR})
|
||||
@@ -0,0 +1,68 @@
|
||||
#include "DirectMemory.hpp"
|
||||
#include "MemoryPool.hpp"
|
||||
#include <sys/mman.h>
|
||||
|
||||
static int LinuxProtFromSce(int prot) {
|
||||
int result = PROT_NONE;
|
||||
if (prot & 1) result |= PROT_READ;
|
||||
if (prot & 2) result |= PROT_WRITE;
|
||||
if (prot & 4) result |= PROT_EXEC;
|
||||
return result;
|
||||
}
|
||||
|
||||
int DoMapDirect(void** addr, size_t len, int prot, int flags, int64_t physStart, size_t alignment) {
|
||||
(void)alignment;
|
||||
if (!addr || len == 0 || (len & (PS5_PAGE_SIZE - 1)) || physStart < 0
|
||||
|| (static_cast<uint64_t>(physStart) & (PS5_PAGE_SIZE - 1)))
|
||||
return SCE_KERNEL_ERROR_EINVAL;
|
||||
constexpr int GUEST_MAP_FIXED = 0x10;
|
||||
bool fixed = (flags & GUEST_MAP_FIXED) != 0;
|
||||
int mmapFlags = MAP_PRIVATE | MAP_ANONYMOUS;
|
||||
void* hint = fixed ? *addr : nullptr;
|
||||
if (fixed) mmapFlags |= MAP_FIXED;
|
||||
void* result = mmap(hint, len, LinuxProtFromSce(prot), mmapFlags, -1, 0);
|
||||
if (result == MAP_FAILED) return SCE_KERNEL_ERROR_ENOMEM;
|
||||
*addr = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DoMapAnon(void** addr, size_t len, int prot, int flags) {
|
||||
if (!addr || len == 0 || (len & (PS5_PAGE_SIZE - 1))) return SCE_KERNEL_ERROR_EINVAL;
|
||||
constexpr int GUEST_MAP_FIXED = 0x10;
|
||||
bool fixed = (flags & GUEST_MAP_FIXED) != 0;
|
||||
int mmapFlags = MAP_PRIVATE | MAP_ANONYMOUS;
|
||||
void* hint = fixed ? *addr : nullptr;
|
||||
if (fixed) mmapFlags |= MAP_FIXED;
|
||||
void* result = mmap(hint, len, LinuxProtFromSce(prot), mmapFlags, -1, 0);
|
||||
if (result == MAP_FAILED) return SCE_KERNEL_ERROR_ENOMEM;
|
||||
*addr = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DoMprotect(const void* addr, size_t len, int prot) {
|
||||
if (mprotect(const_cast<void*>(addr), len, LinuxProtFromSce(prot)) != 0) return SCE_KERNEL_ERROR_EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DoMunmap(void* addr, size_t len) {
|
||||
munmap(addr, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DoReserveVirtual(void** addr, size_t len, size_t alignment) {
|
||||
if (!addr || len == 0) return SCE_KERNEL_ERROR_EINVAL;
|
||||
size_t allocLen = alignment ? len + alignment : len;
|
||||
void* result = mmap(nullptr, allocLen, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
if (result == MAP_FAILED) return SCE_KERNEL_ERROR_ENOMEM;
|
||||
if (alignment) {
|
||||
uintptr_t raw = reinterpret_cast<uintptr_t>(result);
|
||||
uintptr_t aligned = (raw + alignment - 1) & ~(alignment - 1);
|
||||
if (aligned != raw) munmap(result, aligned - raw);
|
||||
uintptr_t end = aligned + len;
|
||||
uintptr_t rawEnd = raw + allocLen;
|
||||
if (end < rawEnd) munmap(reinterpret_cast<void*>(end), rawEnd - end);
|
||||
result = reinterpret_cast<void*>(aligned);
|
||||
}
|
||||
*addr = result;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
|
||||
static constexpr size_t DIRECT_MEMORY_SIZE = 13824ULL * 1024 * 1024;
|
||||
static constexpr size_t PS5_PAGE_SIZE = 0x4000;
|
||||
|
||||
static constexpr int SCE_KERNEL_ERROR_EINVAL = -2147418107;
|
||||
static constexpr int SCE_KERNEL_ERROR_EAGAIN = -2147418110;
|
||||
static constexpr int SCE_KERNEL_ERROR_ENOMEM = -2147418105;
|
||||
static constexpr int SCE_KERNEL_ERROR_EACCES = -2147418108;
|
||||
static constexpr int SCE_KERNEL_ERROR_EFAULT = -2147418103;
|
||||
|
||||
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);
|
||||
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);
|
||||
int DoMunmap(void* addr, size_t len);
|
||||
int DoReserveVirtual(void** addr, size_t len, size_t alignment);
|
||||
@@ -1,7 +1,9 @@
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include "SceTypes.hpp"
|
||||
#include "prx/libc/include/General.hpp"
|
||||
#include "DirectMemory.hpp"
|
||||
|
||||
extern "C" {
|
||||
|
||||
@@ -673,32 +675,26 @@ int sceKernelAioWaitRequest(int32_t id, int32_t* state, uint32_t* usec) {
|
||||
}
|
||||
|
||||
int sceKernelAllocateDirectMemory(int64_t search_start, int64_t search_end, size_t len, size_t alignment, int memory_type, int64_t* phys_addr_out) {
|
||||
(void)search_start;
|
||||
(void)search_end;
|
||||
(void)len;
|
||||
(void)alignment;
|
||||
(void)memory_type;
|
||||
(void)phys_addr_out;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
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);
|
||||
}
|
||||
|
||||
int sceKernelAllocateMainDirectMemory(size_t len, size_t alignment, int memory_type, int64_t* phys_addr_out) {
|
||||
(void)len;
|
||||
(void)alignment;
|
||||
(void)memory_type;
|
||||
(void)phys_addr_out;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
return sceKernelAllocateDirectMemory(0, static_cast<int64_t>(DIRECT_MEMORY_SIZE), len, alignment, memory_type, phys_addr_out);
|
||||
}
|
||||
|
||||
int sceKernelAvailableDirectMemorySize(int64_t search_start, int64_t search_end, size_t alignment, int64_t* phys_addr_out, size_t* size_out) {
|
||||
(void)search_start;
|
||||
(void)search_end;
|
||||
(void)alignment;
|
||||
(void)phys_addr_out;
|
||||
(void)size_out;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
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);
|
||||
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);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -886,11 +882,14 @@ int sceKernelDeleteUserEvent(KernelEqueue eq, int id) {
|
||||
}
|
||||
|
||||
int sceKernelDirectMemoryQuery(int64_t offset, int flags, void* info, size_t info_size) {
|
||||
(void)offset;
|
||||
(void)flags;
|
||||
(void)info;
|
||||
(void)info_size;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
if (!info || offset < 0) return SCE_KERNEL_ERROR_EINVAL;
|
||||
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;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -929,8 +928,7 @@ int sceKernelGetdents(int fd, char* buf, int nbytes) {
|
||||
}
|
||||
|
||||
size_t sceKernelGetDirectMemorySize(void) {
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
return 13824ULL * 1024 * 1024;
|
||||
}
|
||||
|
||||
int sceKernelGetdirentries(int fd, char* buf, int nbytes, int64_t* basep) {
|
||||
@@ -1109,57 +1107,27 @@ int64_t sceKernelLseek(int d, int64_t offset, int whence) {
|
||||
}
|
||||
|
||||
int sceKernelMapDirectMemory(void** addr, size_t len, int prot, int flags, int64_t direct_memory_start, size_t alignment) {
|
||||
(void)addr;
|
||||
(void)len;
|
||||
(void)prot;
|
||||
(void)flags;
|
||||
(void)direct_memory_start;
|
||||
(void)alignment;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
return DoMapDirect(addr, len, prot, flags, direct_memory_start, alignment);
|
||||
}
|
||||
|
||||
int sceKernelMapDirectMemory2(void** addr, size_t len, int type, int prot, int flags, int64_t direct_memory_start, size_t alignment) {
|
||||
(void)addr;
|
||||
(void)len;
|
||||
(void)type;
|
||||
(void)prot;
|
||||
(void)flags;
|
||||
(void)direct_memory_start;
|
||||
(void)alignment;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
return DoMapDirect(addr, len, prot, flags, direct_memory_start, alignment);
|
||||
}
|
||||
|
||||
int sceKernelMapFlexibleMemory(void** addr_in_out, size_t len, int prot, int flags) {
|
||||
(void)addr_in_out;
|
||||
(void)len;
|
||||
(void)prot;
|
||||
(void)flags;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
return DoMapAnon(addr_in_out, len, prot, flags);
|
||||
}
|
||||
|
||||
int sceKernelMapNamedDirectMemory(void** addr, size_t len, int prot, int flags, int64_t direct_memory_start, size_t alignment, const char* name) {
|
||||
(void)addr;
|
||||
(void)len;
|
||||
(void)prot;
|
||||
(void)flags;
|
||||
(void)direct_memory_start;
|
||||
(void)alignment;
|
||||
(void)name;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
return DoMapDirect(addr, len, prot, flags, direct_memory_start, alignment);
|
||||
}
|
||||
|
||||
int32_t sceKernelMapNamedFlexibleMemory(void** addr_in_out, size_t len, int prot, int flags, const char* name) {
|
||||
(void)addr_in_out;
|
||||
(void)len;
|
||||
(void)prot;
|
||||
(void)flags;
|
||||
(void)name;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
return sceKernelMapFlexibleMemory(addr_in_out, len, prot, flags);
|
||||
}
|
||||
|
||||
int sceKernelMemoryPoolBatch(const KernelMemoryPoolBatchEntry* entries, int num_entries, int* num_entries_out, int flags) {
|
||||
@@ -1224,11 +1192,7 @@ int sceKernelMkdir(const char* path, uint16_t mode) {
|
||||
}
|
||||
|
||||
int sceKernelMprotect(const void* addr, size_t len, int prot) {
|
||||
(void)addr;
|
||||
(void)len;
|
||||
(void)prot;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
return DoMprotect(addr, len, prot);
|
||||
}
|
||||
|
||||
int sceKernelMtypeprotect(const void* addr, size_t len, int type, int prot) {
|
||||
@@ -1241,10 +1205,7 @@ int sceKernelMtypeprotect(const void* addr, size_t len, int type, int prot) {
|
||||
}
|
||||
|
||||
int sceKernelMunmap(uint64_t vaddr, size_t len) {
|
||||
(void)vaddr;
|
||||
(void)len;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
return DoMunmap(reinterpret_cast<void*>(vaddr), len);
|
||||
}
|
||||
|
||||
int sceKernelNanosleep(const KernelTimespec* rqtp, KernelTimespec* rmtp) {
|
||||
@@ -1326,9 +1287,8 @@ uint64_t sceKernelReadTsc(void) {
|
||||
}
|
||||
|
||||
int sceKernelReleaseDirectMemory(int64_t start, size_t len) {
|
||||
(void)start;
|
||||
(void)len;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
if (start < 0 || len == 0) return SCE_KERNEL_ERROR_EINVAL;
|
||||
DirectMemoryFree(start, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1346,12 +1306,8 @@ int sceKernelRename(const char* from, const char* to) {
|
||||
}
|
||||
|
||||
int sceKernelReserveVirtualRange(void** addr, size_t len, int flags, size_t alignment) {
|
||||
(void)addr;
|
||||
(void)len;
|
||||
(void)flags;
|
||||
(void)alignment;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
return DoReserveVirtual(addr, len, alignment);
|
||||
}
|
||||
|
||||
int sceKernelRmdir(const char* path) {
|
||||
@@ -1482,11 +1438,14 @@ int sceKernelUuidCreate(uint32_t* uuid) {
|
||||
}
|
||||
|
||||
int sceKernelVirtualQuery(const void* addr, int flags, VirtualQueryInfo* info, uint64_t info_size) {
|
||||
(void)addr;
|
||||
(void)flags;
|
||||
(void)info;
|
||||
(void)info_size;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
if (!info || info_size < sizeof(VirtualQueryInfo)) return SCE_KERNEL_ERROR_EINVAL;
|
||||
memset(info, 0, sizeof(VirtualQueryInfo));
|
||||
uintptr_t ptr = reinterpret_cast<uintptr_t>(addr);
|
||||
info->start = ptr & ~static_cast<uintptr_t>(PS5_PAGE_SIZE - 1);
|
||||
info->end = info->start + PS5_PAGE_SIZE;
|
||||
info->is_direct = 1;
|
||||
info->protection = 3;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
#define _GLIBCXX_HAS_GTHREADS 0
|
||||
#include "MemoryPool.hpp"
|
||||
#include "DirectMemory.hpp"
|
||||
#include <mutex>
|
||||
|
||||
static constexpr size_t NUM_PAGES = DIRECT_MEMORY_SIZE / PS5_PAGE_SIZE;
|
||||
|
||||
struct PhysicalMemoryPool {
|
||||
static PhysicalMemoryPool& Instance() {
|
||||
static PhysicalMemoryPool inst;
|
||||
return inst;
|
||||
}
|
||||
|
||||
int Alloc(int64_t searchStart, int64_t searchEnd, size_t len, size_t alignment, 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);
|
||||
uint64_t end = static_cast<uint64_t>(searchEnd);
|
||||
uint64_t cur = (start + align - 1) & ~(align - 1);
|
||||
while (cur + len <= end && cur + len <= DIRECT_MEMORY_SIZE) {
|
||||
if (_isFree(cur, len)) {
|
||||
_mark(cur, len, true);
|
||||
*physOut = static_cast<int64_t>(cur);
|
||||
return 0;
|
||||
}
|
||||
cur += align;
|
||||
}
|
||||
return SCE_KERNEL_ERROR_EAGAIN;
|
||||
}
|
||||
|
||||
void Free(uint64_t start, size_t len) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
_mark(start, len, false);
|
||||
}
|
||||
|
||||
private:
|
||||
bool _isFree(uint64_t offset, size_t len) const {
|
||||
size_t first = offset / PS5_PAGE_SIZE;
|
||||
size_t count = len / PS5_PAGE_SIZE;
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
if (_used[first + i]) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void _mark(uint64_t offset, size_t len, bool used) {
|
||||
size_t first = offset / PS5_PAGE_SIZE;
|
||||
size_t count = len / PS5_PAGE_SIZE;
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
_used[first + i] = used;
|
||||
}
|
||||
|
||||
std::mutex _mutex;
|
||||
bool _used[NUM_PAGES] = {};
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void DirectMemoryFree(int64_t start, size_t len) {
|
||||
PhysicalMemoryPool::Instance().Free(static_cast<uint64_t>(start), len);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#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);
|
||||
Reference in New Issue
Block a user