intial flexible_memory support. Added sceKernelMapNamedFlexibleMemory

This commit is contained in:
georgemoralis 2024-04-20 09:22:40 +03:00
parent fbd530408b
commit b04bc932a1
5 changed files with 117 additions and 4 deletions

View file

@ -107,7 +107,9 @@ set(GNM_LIB src/core/libraries/gnmdriver/gnmdriver.cpp
src/core/libraries/gnmdriver/gnmdriver.h
)
set(KERNEL_LIB src/core/libraries/kernel/memory/kernel_memory.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

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,34 @@
// 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/PS4/GPU/gpu_memory.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

@ -1,14 +1,68 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "kernel_memory.h"
#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 {
s32 PS4_SYSV_ABI sceKernelMapNamedFlexibleMemory(void** addrInOut, std::size_t len, int prot,
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) {
return 0;
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;
}
void RegisterKernelMemory(Core::Loader::SymbolsResolver* sym) {

View file

@ -902,6 +902,7 @@ void pthreadSymbolsRegister(Core::Loader::SymbolsResolver* sym) {
LIB_FUNCTION("bt3CTBKmGyI", "libkernel", 1, "libkernel", 1, 1, scePthreadSetaffinity);
LIB_FUNCTION("6UgtwV+0zb4", "libkernel", 1, "libkernel", 1, 1, scePthreadCreate);
LIB_FUNCTION("T72hz6ffq08", "libkernel", 1, "libkernel", 1, 1, scePthreadYield);
LIB_FUNCTION("UTXzJbWhhTE", "libkernel", 1, "libkernel", 1, 1, scePthreadAttrSetstacksize);
// mutex calls
LIB_FUNCTION("cmo1RIYva9o", "libkernel", 1, "libkernel", 1, 1, scePthreadMutexInit);