added stubbed mostly version of flexible memory - it allows sonic mania to go further

This commit is contained in:
georgemoralis 2024-05-02 17:13:51 +03:00
parent 2f9f4e1eeb
commit 90e3e47c4d
6 changed files with 158 additions and 1 deletions

View File

@ -101,7 +101,11 @@ set(GNM_LIB src/core/libraries/gnmdriver/gnmdriver.cpp
src/core/libraries/gnmdriver/gnmdriver.h
)
set(KERNEL_LIB src/core/libraries/kernel/cpu_management.cpp
set(KERNEL_LIB src/core/libraries/kernel/memory/flexible_memory.cpp
src/core/libraries/kernel/memory/flexible_memory.h
src/core/libraries/kernel/memory/kernel_memory.cpp
src/core/libraries/kernel/memory/kernel_memory.h
src/core/libraries/kernel/cpu_management.cpp
src/core/libraries/kernel/cpu_management.h
src/core/libraries/kernel/event_queue.cpp
src/core/libraries/kernel/event_queue.h

View File

@ -10,6 +10,7 @@
#include "core/libraries/kernel/event_queues.h"
#include "core/libraries/kernel/file_system.h"
#include "core/libraries/kernel/libkernel.h"
#include "core/libraries/kernel/memory/kernel_memory.h"
#include "core/libraries/kernel/memory_management.h"
#include "core/libraries/kernel/thread_management.h"
#include "core/libraries/kernel/time_management.h"
@ -180,6 +181,7 @@ void LibKernel_Register(Core::Loader::SymbolsResolver* sym) {
Libraries::Kernel::fileSystemSymbolsRegister(sym);
Libraries::Kernel::timeSymbolsRegister(sym);
Libraries::Kernel::pthreadSymbolsRegister(sym);
Libraries::Kernel::RegisterKernelMemory(sym);
// temp
LIB_FUNCTION("NWtTN10cJzE", "libSceLibcInternalExt", 1, "libSceLibcInternal", 1, 1,

View File

@ -0,0 +1,22 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "flexible_memory.h"
namespace Libraries::Kernel {
bool FlexibleMemory::Map(u64 virtual_addr, std::size_t len, int prot,
VirtualMemory::MemoryMode cpu_mode) {
std::scoped_lock lock{mutex};
AllocatedBlock block{};
block.map_virtual_addr = virtual_addr;
block.map_size = len;
block.prot = prot;
block.cpu_mode = cpu_mode;
allocated_blocks.push_back(block);
allocated_total += len;
return true;
}
} // namespace Libraries::Kernel

View File

@ -0,0 +1,33 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <mutex>
#include <vector>
#include "common/types.h"
#include "core/virtual_memory.h"
namespace Libraries::Kernel {
class FlexibleMemory {
public:
struct AllocatedBlock {
u64 map_virtual_addr;
u64 map_size;
int prot;
VirtualMemory::MemoryMode cpu_mode;
};
FlexibleMemory(){};
virtual ~FlexibleMemory(){};
public:
bool Map(u64 virtual_addr, std::size_t len, int prot, VirtualMemory::MemoryMode cpu_mode);
private:
std::vector<AllocatedBlock> allocated_blocks;
u64 allocated_total = 0;
std::mutex mutex;
};
} // namespace Libraries::Kernel

View File

@ -0,0 +1,78 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <common/assert.h>
#include <common/singleton.h>
#include <core/libraries/error_codes.h>
#include <core/libraries/libs.h>
#include <core/virtual_memory.h>
#include "flexible_memory.h"
#include "kernel_memory.h"
namespace Libraries::Kernel {
bool Is16KBMultiple(u64 n) {
return ((n % (16ull * 1024) == 0));
}
s32 PS4_SYSV_ABI sceKernelMapNamedFlexibleMemory(void** addr_in_out, std::size_t len, int prot,
int flags, const char* name) {
LOG_INFO(Kernel_Vmm, "len = {:#x}, prot = {:#x}, flags = {:#x}, name = {}", len, prot, flags,
name);
if (len == 0 || !Is16KBMultiple(len)) {
LOG_ERROR(Kernel_Vmm, "len is 0 or not 16kb multiple");
return ORBIS_KERNEL_ERROR_EINVAL;
}
static constexpr size_t MaxNameSize = 32;
if (std::strlen(name) > MaxNameSize) {
LOG_ERROR(Kernel_Vmm, "name exceeds 32 bytes!");
return ORBIS_KERNEL_ERROR_ENAMETOOLONG;
}
if (name == nullptr) {
LOG_ERROR(Kernel_Vmm, "name is invalid!");
return ORBIS_KERNEL_ERROR_EFAULT;
}
VirtualMemory::MemoryMode cpu_mode = VirtualMemory::MemoryMode::NoAccess;
switch (prot) {
case 0x3:
cpu_mode = VirtualMemory::MemoryMode::ReadWrite;
break;
default:
UNREACHABLE();
}
auto in_addr = reinterpret_cast<u64>(*addr_in_out);
auto out_addr = VirtualMemory::memory_alloc(in_addr, len, cpu_mode);
*addr_in_out = reinterpret_cast<void*>(out_addr);
auto* flexible_memory = Common::Singleton<FlexibleMemory>::Instance();
if (!flexible_memory->Map(out_addr, len, prot, cpu_mode)) {
UNREACHABLE();
}
if (out_addr == 0) {
LOG_ERROR(Kernel_Vmm, "Can't allocate address");
return ORBIS_KERNEL_ERROR_ENOMEM;
}
LOG_INFO(Kernel_Vmm, "in_addr = {:#x} out_addr = {:#x}", in_addr, out_addr);
return ORBIS_OK;
}
s32 PS4_SYSV_ABI sceKernelMapFlexibleMemory(void** addr_in_out, std::size_t len, int prot,
int flags) {
return sceKernelMapNamedFlexibleMemory(addr_in_out, len, prot, flags, "");
}
void RegisterKernelMemory(Core::Loader::SymbolsResolver* sym) {
LIB_FUNCTION("mL8NDH86iQI", "libkernel", 1, "libkernel", 1, 1, sceKernelMapNamedFlexibleMemory);
LIB_FUNCTION("IWIBBdTHit4", "libkernel", 1, "libkernel", 1, 1, sceKernelMapFlexibleMemory);
}
} // namespace Libraries::Kernel

View File

@ -0,0 +1,18 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "common/types.h"
namespace Core::Loader {
class SymbolsResolver;
}
namespace Libraries::Kernel {
s32 PS4_SYSV_ABI sceKernelMapNamedFlexibleMemory(void** addrInOut, std::size_t len, int prot,
int flags, const char* name);
s32 PS4_SYSV_ABI sceKernelMapFlexibleMemory(void** addr_in_out, std::size_t len, int prot,
int flags);
void RegisterKernelMemory(Core::Loader::SymbolsResolver* sym);
} // namespace Libraries::Kernel