refactoring virtual_memory

This commit is contained in:
georgemoralis 2023-08-06 22:52:20 +03:00
parent 1e57ccc6ec
commit 3cb89c4b5c
5 changed files with 30 additions and 36 deletions

View File

@ -31,8 +31,8 @@ add_executable(shadps4
src/GUI/ElfViewer.h src/GUI/ElfViewer.h
src/Util/Log.h src/Util/Log.h
src/Util/Log.cpp src/Util/Log.cpp
src/Core/Memory.cpp "src/Core/virtual_memory.cpp"
src/Core/Memory.h "src/Core/virtual_memory.h"
src/Core/PS4/Linker.cpp src/Core/PS4/Linker.cpp
src/Core/PS4/Linker.h src/Core/PS4/Linker.h
src/Lib/Threads.cpp src/Lib/Threads.cpp

View File

@ -1,27 +0,0 @@
#pragma once
#include "../types.h"
constexpr u64 SYSTEM_RESERVED = 0x800000000u;
constexpr u64 CODE_BASE_OFFSET = 0x100000000u;
namespace Memory
{
enum class MemoryMode : u32
{
NoAccess = 0,
Read = 1,
Write = 2,
ReadWrite =3,
Execute = 4,
ExecuteRead = 5,
ExecuteWrite = 6,
ExecuteReadWrite = 7,
};
namespace VirtualMemory {
u64 memory_alloc(u64 address, u64 size, MemoryMode mode);
bool memory_protect(u64 address, u64 size, MemoryMode mode, MemoryMode* old_mode);
bool memory_flush(u64 address, u64 size);
bool memory_patch(u64 vaddr, u64 value);
}
}

View File

@ -1,5 +1,5 @@
#include "Linker.h" #include "Linker.h"
#include "../Memory.h" #include "../virtual_memory.h"
#include "../../Util/Log.h" #include "../../Util/Log.h"
#include "../../Util/Disassembler.h" #include "../../Util/Disassembler.h"
#include <Util/string_util.h> #include <Util/string_util.h>
@ -112,7 +112,7 @@ void Linker::LoadModuleToMemory(Module* m)
u64 base_size = calculate_base_size(elf_header, elf_pheader); u64 base_size = calculate_base_size(elf_header, elf_pheader);
m->aligned_base_size = (base_size & ~(static_cast<u64>(0x1000) - 1)) + 0x1000;//align base size to 0x1000 block size (TODO is that the default block size or it can be changed? m->aligned_base_size = (base_size & ~(static_cast<u64>(0x1000) - 1)) + 0x1000;//align base size to 0x1000 block size (TODO is that the default block size or it can be changed?
m->base_virtual_addr = Memory::VirtualMemory::memory_alloc(g_load_addr, m->aligned_base_size, Memory::MemoryMode::ExecuteReadWrite); m->base_virtual_addr = VirtualMemory::memory_alloc(g_load_addr, m->aligned_base_size, VirtualMemory::MemoryMode::ExecuteReadWrite);
LOG_INFO_IF(debug_loader, "====Load Module to Memory ========\n"); LOG_INFO_IF(debug_loader, "====Load Module to Memory ========\n");
LOG_INFO_IF(debug_loader, "base_virtual_addr ......: {:#018x}\n", m->base_virtual_addr); LOG_INFO_IF(debug_loader, "base_virtual_addr ......: {:#018x}\n", m->base_virtual_addr);
@ -566,7 +566,7 @@ static void relocate(u32 idx, elf_relocation* rel, Module* m, bool isJmpRel) {
} }
if (rel_isResolved) { if (rel_isResolved) {
Memory::VirtualMemory::memory_patch(rel_virtual_addr, rel_value); VirtualMemory::memory_patch(rel_virtual_addr, rel_value);
} }
else else
{ {

View File

@ -1,6 +1,6 @@
#include "Memory.h" #include "virtual_memory.h"
#include "../Core/PS4/Loader/Elf.h" #include "Core/PS4/Loader/Elf.h"
#ifdef _WIN64 #ifdef _WIN64
#include <windows.h> #include <windows.h>
@ -21,7 +21,6 @@ enum PosixPageProtection {
#include "../Util/Log.h" #include "../Util/Log.h"
namespace Memory {
namespace VirtualMemory { namespace VirtualMemory {
static u32 convertMemoryMode(MemoryMode mode) { static u32 convertMemoryMode(MemoryMode mode) {
switch (mode) { switch (mode) {
@ -119,4 +118,3 @@ bool memory_patch(u64 vaddr, u64 value) {
return ret; return ret;
} }
} // namespace VirtualMemory } // namespace VirtualMemory
} // namespace Memory

23
src/Core/virtual_memory.h Normal file
View File

@ -0,0 +1,23 @@
#pragma once
#include <types.h>
constexpr u64 SYSTEM_RESERVED = 0x800000000u;
constexpr u64 CODE_BASE_OFFSET = 0x100000000u;
namespace VirtualMemory {
enum class MemoryMode : u32 {
NoAccess = 0,
Read = 1,
Write = 2,
ReadWrite = 3,
Execute = 4,
ExecuteRead = 5,
ExecuteWrite = 6,
ExecuteReadWrite = 7,
};
u64 memory_alloc(u64 address, u64 size, MemoryMode mode);
bool memory_protect(u64 address, u64 size, MemoryMode mode, MemoryMode* old_mode);
bool memory_flush(u64 address, u64 size);
bool memory_patch(u64 vaddr, u64 value);
} // namespace VirtualMemory