2024-02-23 22:32:32 +01:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
2023-11-06 00:11:54 +01:00
|
|
|
#include "core/virtual_memory.h"
|
2023-08-03 10:29:14 +02:00
|
|
|
|
2023-11-21 15:54:10 +01:00
|
|
|
#include "common/log.h"
|
|
|
|
|
2023-06-23 03:48:55 +02:00
|
|
|
#ifdef _WIN64
|
2023-08-03 10:29:14 +02:00
|
|
|
#include <windows.h>
|
2023-06-23 03:48:55 +02:00
|
|
|
#else
|
2023-08-03 10:29:14 +02:00
|
|
|
#include <sys/mman.h>
|
2023-06-23 03:48:55 +02:00
|
|
|
#endif
|
|
|
|
|
2023-07-07 12:49:46 +02:00
|
|
|
#if !defined(_WIN64)
|
2023-08-03 10:29:14 +02:00
|
|
|
enum PosixPageProtection {
|
|
|
|
PAGE_NOACCESS = 0,
|
|
|
|
PAGE_READONLY = PROT_READ,
|
|
|
|
PAGE_READWRITE = PROT_READ | PROT_WRITE,
|
|
|
|
PAGE_EXECUTE = PROT_EXEC,
|
|
|
|
PAGE_EXECUTE_READ = PROT_EXEC | PROT_READ,
|
2023-07-07 12:49:46 +02:00
|
|
|
PAGE_EXECUTE_READWRITE = PROT_EXEC | PROT_READ | PROT_WRITE
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2023-08-03 10:29:14 +02:00
|
|
|
namespace VirtualMemory {
|
|
|
|
static u32 convertMemoryMode(MemoryMode mode) {
|
|
|
|
switch (mode) {
|
2024-02-23 21:57:57 +01:00
|
|
|
case MemoryMode::Read:
|
|
|
|
return PAGE_READONLY;
|
|
|
|
case MemoryMode::Write:
|
|
|
|
case MemoryMode::ReadWrite:
|
|
|
|
return PAGE_READWRITE;
|
|
|
|
|
|
|
|
case MemoryMode::Execute:
|
|
|
|
return PAGE_EXECUTE;
|
|
|
|
case MemoryMode::ExecuteRead:
|
|
|
|
return PAGE_EXECUTE_READ;
|
|
|
|
case MemoryMode::ExecuteWrite:
|
|
|
|
case MemoryMode::ExecuteReadWrite:
|
|
|
|
return PAGE_EXECUTE_READWRITE;
|
|
|
|
|
|
|
|
case MemoryMode::NoAccess:
|
|
|
|
return PAGE_NOACCESS;
|
|
|
|
default:
|
|
|
|
return PAGE_NOACCESS;
|
2023-08-03 10:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
static MemoryMode convertMemoryMode(u32 mode) {
|
|
|
|
switch (mode) {
|
2024-02-23 21:57:57 +01:00
|
|
|
case PAGE_NOACCESS:
|
|
|
|
return MemoryMode::NoAccess;
|
|
|
|
case PAGE_READONLY:
|
|
|
|
return MemoryMode::Read;
|
|
|
|
case PAGE_READWRITE:
|
|
|
|
return MemoryMode::ReadWrite;
|
|
|
|
case PAGE_EXECUTE:
|
|
|
|
return MemoryMode::Execute;
|
|
|
|
case PAGE_EXECUTE_READ:
|
|
|
|
return MemoryMode::ExecuteRead;
|
|
|
|
case PAGE_EXECUTE_READWRITE:
|
|
|
|
return MemoryMode::ExecuteReadWrite;
|
|
|
|
default:
|
|
|
|
return MemoryMode::NoAccess;
|
2023-08-03 10:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 memory_alloc(u64 address, u64 size, MemoryMode mode) {
|
|
|
|
#ifdef _WIN64
|
2024-02-23 21:57:57 +01:00
|
|
|
auto ptr = reinterpret_cast<uintptr_t>(VirtualAlloc(
|
|
|
|
reinterpret_cast<LPVOID>(static_cast<uintptr_t>(address)), size,
|
|
|
|
static_cast<DWORD>(MEM_COMMIT) | static_cast<DWORD>(MEM_RESERVE), convertMemoryMode(mode)));
|
2023-08-03 10:29:14 +02:00
|
|
|
|
|
|
|
if (ptr == 0) {
|
|
|
|
auto err = static_cast<u32>(GetLastError());
|
|
|
|
LOG_ERROR_IF(true, "VirtualAlloc() failed: 0x{:X}\n", err);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
auto ptr = reinterpret_cast<uintptr_t>(
|
2024-02-23 21:57:57 +01:00
|
|
|
mmap(reinterpret_cast<void*>(static_cast<uintptr_t>(address)), size,
|
|
|
|
PROT_EXEC | PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0));
|
2023-08-03 10:29:14 +02:00
|
|
|
|
|
|
|
if (ptr == reinterpret_cast<uintptr_t> MAP_FAILED) {
|
|
|
|
LOG_ERROR_IF(true, "mmap() failed: {}\n", std::strerror(errno));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
bool memory_protect(u64 address, u64 size, MemoryMode mode, MemoryMode* old_mode) {
|
|
|
|
#ifdef _WIN64
|
|
|
|
DWORD old_protect = 0;
|
2024-02-23 21:57:57 +01:00
|
|
|
if (VirtualProtect(reinterpret_cast<LPVOID>(static_cast<uintptr_t>(address)), size,
|
|
|
|
convertMemoryMode(mode), &old_protect) == 0) {
|
2023-08-03 10:29:14 +02:00
|
|
|
auto err = static_cast<u32>(GetLastError());
|
|
|
|
LOG_ERROR_IF(true, "VirtualProtect() failed: 0x{:X}\n", err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (old_mode != nullptr) {
|
|
|
|
*old_mode = convertMemoryMode(old_protect);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
#else
|
2024-02-14 23:52:57 +01:00
|
|
|
int ret = mprotect(reinterpret_cast<void*>(address), size, convertMemoryMode(mode));
|
|
|
|
return true;
|
2023-08-03 10:29:14 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool memory_flush(u64 address, u64 size) {
|
|
|
|
#ifdef _WIN64
|
2024-02-23 21:57:57 +01:00
|
|
|
if (::FlushInstructionCache(GetCurrentProcess(),
|
|
|
|
reinterpret_cast<LPVOID>(static_cast<uintptr_t>(address)),
|
|
|
|
size) == 0) {
|
2023-08-03 10:29:14 +02:00
|
|
|
auto err = static_cast<u32>(GetLastError());
|
|
|
|
LOG_ERROR_IF(true, "FlushInstructionCache() failed: 0x{:X}\n", err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2024-02-23 21:57:57 +01:00
|
|
|
#else // linux probably doesn't have something similar
|
2023-08-03 10:29:14 +02:00
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
bool memory_patch(u64 vaddr, u64 value) {
|
|
|
|
MemoryMode old_mode{};
|
|
|
|
memory_protect(vaddr, 8, MemoryMode::ReadWrite, &old_mode);
|
|
|
|
|
|
|
|
auto* ptr = reinterpret_cast<uint64_t*>(vaddr);
|
|
|
|
|
|
|
|
bool ret = (*ptr != value);
|
|
|
|
|
|
|
|
*ptr = value;
|
|
|
|
|
|
|
|
memory_protect(vaddr, 8, old_mode, nullptr);
|
|
|
|
|
|
|
|
// if mode is executable flush it so insure that cpu finds it
|
2023-11-21 15:54:10 +01:00
|
|
|
if (containsExecuteMode(old_mode)) {
|
2023-08-03 10:29:14 +02:00
|
|
|
memory_flush(vaddr, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2023-06-23 03:48:55 +02:00
|
|
|
}
|
2024-02-23 21:57:57 +01:00
|
|
|
static u64 AlignUp(u64 pos, u64 align) {
|
|
|
|
return (align != 0 ? (pos + (align - 1)) & ~(align - 1) : pos);
|
|
|
|
}
|
2023-08-07 17:16:26 +02:00
|
|
|
|
|
|
|
u64 memory_alloc_aligned(u64 address, u64 size, MemoryMode mode, u64 alignment) {
|
2024-02-14 23:52:57 +01:00
|
|
|
#ifdef _WIN64
|
2023-08-07 17:16:26 +02:00
|
|
|
// try allocate aligned address inside user area
|
|
|
|
MEM_ADDRESS_REQUIREMENTS req{};
|
|
|
|
MEM_EXTENDED_PARAMETER param{};
|
2024-02-23 21:57:57 +01:00
|
|
|
req.LowestStartingAddress =
|
|
|
|
(address == 0 ? reinterpret_cast<PVOID>(USER_MIN)
|
|
|
|
: reinterpret_cast<PVOID>(AlignUp(address, alignment)));
|
2023-08-07 17:16:26 +02:00
|
|
|
req.HighestEndingAddress = reinterpret_cast<PVOID>(USER_MAX);
|
|
|
|
req.Alignment = alignment;
|
|
|
|
param.Type = MemExtendedParameterAddressRequirements;
|
|
|
|
param.Pointer = &req;
|
|
|
|
|
2024-02-23 21:57:57 +01:00
|
|
|
auto ptr = reinterpret_cast<uintptr_t>(
|
|
|
|
VirtualAlloc2(GetCurrentProcess(), nullptr, size,
|
|
|
|
static_cast<DWORD>(MEM_COMMIT) | static_cast<DWORD>(MEM_RESERVE),
|
|
|
|
convertMemoryMode(mode), ¶m, 1));
|
2023-08-07 17:16:26 +02:00
|
|
|
|
|
|
|
if (ptr == 0) {
|
|
|
|
auto err = static_cast<u32>(GetLastError());
|
|
|
|
LOG_ERROR_IF(true, "VirtualAlloc2() failed: 0x{:X}\n", err);
|
|
|
|
}
|
|
|
|
return ptr;
|
2024-02-14 23:52:57 +01:00
|
|
|
#else
|
|
|
|
void* hint_address = reinterpret_cast<void*>(AlignUp(address, alignment));
|
|
|
|
void* ptr = mmap(hint_address, size, convertMemoryMode(mode), MAP_ANON | MAP_PRIVATE, -1, 0);
|
|
|
|
if (ptr == MAP_FAILED) {
|
|
|
|
std::abort();
|
|
|
|
}
|
|
|
|
return reinterpret_cast<u64>(ptr);
|
|
|
|
#endif
|
2023-08-07 17:16:26 +02:00
|
|
|
}
|
2024-02-23 21:57:57 +01:00
|
|
|
} // namespace VirtualMemory
|