placeholder for making Memory and Timer classes portable

This commit is contained in:
georgemoralis 2023-08-03 11:29:14 +03:00
parent 0f85cbe54f
commit 1cd0489dfe
2 changed files with 137 additions and 119 deletions

View File

@ -1,6 +1,7 @@
#include "../Core/PS4/Loader/Elf.h"
#include "Memory.h" #include "Memory.h"
#include "../Core/PS4/Loader/Elf.h"
#ifdef _WIN64 #ifdef _WIN64
#include <windows.h> #include <windows.h>
#else #else
@ -8,8 +9,7 @@
#endif #endif
#if !defined(_WIN64) #if !defined(_WIN64)
enum PosixPageProtection enum PosixPageProtection {
{
PAGE_NOACCESS = 0, PAGE_NOACCESS = 0,
PAGE_READONLY = PROT_READ, PAGE_READONLY = PROT_READ,
PAGE_READWRITE = PROT_READ | PROT_WRITE, PAGE_READWRITE = PROT_READ | PROT_WRITE,
@ -21,13 +21,10 @@ enum PosixPageProtection
#include "../Util/Log.h" #include "../Util/Log.h"
namespace Memory namespace Memory {
{
namespace VirtualMemory { namespace VirtualMemory {
static u32 convertMemoryMode(MemoryMode mode) static u32 convertMemoryMode(MemoryMode mode) {
{ switch (mode) {
switch (mode)
{
case MemoryMode::Read: return PAGE_READONLY; case MemoryMode::Read: return PAGE_READONLY;
case MemoryMode::Write: case MemoryMode::Write:
case MemoryMode::ReadWrite: return PAGE_READWRITE; case MemoryMode::ReadWrite: return PAGE_READWRITE;
@ -38,8 +35,7 @@ namespace Memory
case MemoryMode::ExecuteReadWrite: return PAGE_EXECUTE_READWRITE; case MemoryMode::ExecuteReadWrite: return PAGE_EXECUTE_READWRITE;
case MemoryMode::NoAccess: return PAGE_NOACCESS; case MemoryMode::NoAccess: return PAGE_NOACCESS;
default: default: return PAGE_NOACCESS;
return PAGE_NOACCESS;
} }
} }
static MemoryMode convertMemoryMode(u32 mode) { static MemoryMode convertMemoryMode(u32 mode) {
@ -54,35 +50,27 @@ namespace Memory
} }
} }
u64 memory_alloc(u64 address, u64 size, MemoryMode mode) u64 memory_alloc(u64 address, u64 size, MemoryMode mode) {
{
#ifdef _WIN64 #ifdef _WIN64
auto ptr = reinterpret_cast<uintptr_t>(VirtualAlloc(reinterpret_cast<LPVOID>(static_cast<uintptr_t>(address)), auto ptr = reinterpret_cast<uintptr_t>(VirtualAlloc(reinterpret_cast<LPVOID>(static_cast<uintptr_t>(address)), size,
size, static_cast<DWORD>(MEM_COMMIT) | static_cast<DWORD>(MEM_RESERVE), convertMemoryMode(mode)));
static_cast<DWORD>(MEM_COMMIT) | static_cast<DWORD>(MEM_RESERVE),
convertMemoryMode(mode)));
if (ptr == 0) if (ptr == 0) {
{
auto err = static_cast<u32>(GetLastError()); auto err = static_cast<u32>(GetLastError());
LOG_ERROR_IF(true, "VirtualAlloc() failed: 0x{:X}\n", err); LOG_ERROR_IF(true, "VirtualAlloc() failed: 0x{:X}\n", err);
} }
#else #else
auto ptr = reinterpret_cast<uintptr_t>(mmap(reinterpret_cast<void *>(static_cast<uintptr_t>(address)), auto ptr = reinterpret_cast<uintptr_t>(
size, mmap(reinterpret_cast<void*>(static_cast<uintptr_t>(address)), size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0));
PROT_EXEC | PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE,
-1,
0));
if (ptr == reinterpret_cast<uintptr_t>MAP_FAILED) if (ptr == reinterpret_cast<uintptr_t> MAP_FAILED) {
{
LOG_ERROR_IF(true, "mmap() failed: {}\n", std::strerror(errno)); LOG_ERROR_IF(true, "mmap() failed: {}\n", std::strerror(errno));
} }
#endif #endif
return ptr; return ptr;
} }
bool memory_protect(u64 address, u64 size, MemoryMode mode, MemoryMode* old_mode) { bool memory_protect(u64 address, u64 size, MemoryMode mode, MemoryMode* old_mode) {
#ifdef _WIN64
DWORD old_protect = 0; DWORD old_protect = 0;
if (VirtualProtect(reinterpret_cast<LPVOID>(static_cast<uintptr_t>(address)), size, convertMemoryMode(mode), &old_protect) == 0) { if (VirtualProtect(reinterpret_cast<LPVOID>(static_cast<uintptr_t>(address)), size, convertMemoryMode(mode), &old_protect) == 0) {
auto err = static_cast<u32>(GetLastError()); auto err = static_cast<u32>(GetLastError());
@ -93,15 +81,22 @@ namespace Memory
*old_mode = convertMemoryMode(old_protect); *old_mode = convertMemoryMode(old_protect);
} }
return true; return true;
#else
#error Unimplement memory_protect function
#endif
} }
bool memory_flush(u64 address, u64 size) { bool memory_flush(u64 address, u64 size) {
#ifdef _WIN64
if (::FlushInstructionCache(GetCurrentProcess(), reinterpret_cast<LPVOID>(static_cast<uintptr_t>(address)), size) == 0) { if (::FlushInstructionCache(GetCurrentProcess(), reinterpret_cast<LPVOID>(static_cast<uintptr_t>(address)), size) == 0) {
auto err = static_cast<u32>(GetLastError()); auto err = static_cast<u32>(GetLastError());
LOG_ERROR_IF(true, "FlushInstructionCache() failed: 0x{:X}\n", err); LOG_ERROR_IF(true, "FlushInstructionCache() failed: 0x{:X}\n", err);
return false; return false;
} }
return true; return true;
#else // linux probably doesn't have something similar
return true;
#endif
} }
bool memory_patch(u64 vaddr, u64 value) { bool memory_patch(u64 vaddr, u64 value) {
MemoryMode old_mode{}; MemoryMode old_mode{};
@ -123,5 +118,5 @@ namespace Memory
return ret; return ret;
} }
} } // namespace VirtualMemory
} } // namespace Memory

View File

@ -1,33 +1,50 @@
#include "Timer.h" #include "Timer.h"
#ifdef _WIN64
#include <windows.h> #include <windows.h>
#endif
Lib::Timer::Timer() { Lib::Timer::Timer() {
#ifdef _WIN64
LARGE_INTEGER f; LARGE_INTEGER f;
QueryPerformanceFrequency(&f); QueryPerformanceFrequency(&f);
m_Frequency = f.QuadPart; m_Frequency = f.QuadPart;
#else
#error Unimplemented
#endif
} }
void Lib::Timer::Start() { void Lib::Timer::Start() {
#ifdef _WIN64
LARGE_INTEGER c; LARGE_INTEGER c;
QueryPerformanceCounter(&c); QueryPerformanceCounter(&c);
m_StartTime = c.QuadPart; m_StartTime = c.QuadPart;
#else
#error Unimplemented
#endif
m_is_timer_paused = false; m_is_timer_paused = false;
} }
void Lib::Timer::Pause() { void Lib::Timer::Pause() {
#ifdef _WIN64
LARGE_INTEGER c; LARGE_INTEGER c;
QueryPerformanceCounter(&c); QueryPerformanceCounter(&c);
m_PauseTime = c.QuadPart; m_PauseTime = c.QuadPart;
#else
#error Unimplemented
#endif
m_is_timer_paused = true; m_is_timer_paused = true;
} }
void Lib::Timer::Resume() { void Lib::Timer::Resume() {
u64 current_time = 0; u64 current_time = 0;
#ifdef _WIN64
LARGE_INTEGER c; LARGE_INTEGER c;
QueryPerformanceCounter(&c); QueryPerformanceCounter(&c);
current_time = c.QuadPart; current_time = c.QuadPart;
#else
#error Unimplemented
#endif
m_StartTime += current_time - m_PauseTime; m_StartTime += current_time - m_PauseTime;
m_is_timer_paused = false; m_is_timer_paused = false;
} }
@ -40,11 +57,13 @@ double Lib::Timer::GetTimeMsec() const {
} }
u64 current_time = 0; u64 current_time = 0;
#ifdef _WIN64
LARGE_INTEGER c; LARGE_INTEGER c;
QueryPerformanceCounter(&c); QueryPerformanceCounter(&c);
current_time = c.QuadPart; current_time = c.QuadPart;
#else
#error Unimplemented
#endif
return 1000.0 * (static_cast<double>(current_time - m_StartTime)) / static_cast<double>(m_Frequency); return 1000.0 * (static_cast<double>(current_time - m_StartTime)) / static_cast<double>(m_Frequency);
} }
@ -54,11 +73,13 @@ double Lib::Timer::GetTimeSec() const {
} }
u64 current_time = 0; u64 current_time = 0;
#ifdef _WIN64
LARGE_INTEGER c; LARGE_INTEGER c;
QueryPerformanceCounter(&c); QueryPerformanceCounter(&c);
current_time = c.QuadPart; current_time = c.QuadPart;
#else
#error Unimplemented
#endif
return (static_cast<double>(current_time - m_StartTime)) / static_cast<double>(m_Frequency); return (static_cast<double>(current_time - m_StartTime)) / static_cast<double>(m_Frequency);
} }
@ -68,11 +89,13 @@ u64 Lib::Timer::GetTicks() const {
} }
u64 current_time = 0; u64 current_time = 0;
#ifdef _WIN64
LARGE_INTEGER c; LARGE_INTEGER c;
QueryPerformanceCounter(&c); QueryPerformanceCounter(&c);
current_time = c.QuadPart; current_time = c.QuadPart;
#else
#error Unimplemented
#endif
return (current_time - m_StartTime); return (current_time - m_StartTime);
} }