2015-08-26 15:18:01 +00:00
|
|
|
#include "stdafx.h"
|
2015-10-17 17:47:18 +00:00
|
|
|
#include "Utilities/Log.h"
|
2015-08-26 15:18:01 +00:00
|
|
|
#include "VirtualMemory.h"
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#else
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2018-05-13 08:34:33 +00:00
|
|
|
#include <errno.h>
|
2015-08-26 15:18:01 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
|
2018-05-27 10:30:55 +00:00
|
|
|
#ifdef __linux__
|
2018-05-13 19:50:38 +00:00
|
|
|
#include <sys/syscall.h>
|
2018-05-27 10:30:55 +00:00
|
|
|
|
|
|
|
#ifdef __NR_memfd_create
|
|
|
|
#elif __x86_64__
|
|
|
|
#define __NR_memfd_create 319
|
|
|
|
#elif __aarch64__
|
|
|
|
#define __NR_memfd_create 279
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int memfd_create_(const char *name, uint flags)
|
2018-05-13 19:50:38 +00:00
|
|
|
{
|
|
|
|
return syscall(__NR_memfd_create, name, flags);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-03-13 09:40:54 +00:00
|
|
|
namespace utils
|
2015-08-26 15:18:01 +00:00
|
|
|
{
|
2017-03-19 12:50:56 +00:00
|
|
|
// Convert memory protection (internal)
|
|
|
|
static auto operator +(protection prot)
|
2015-08-26 15:18:01 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2017-03-19 12:50:56 +00:00
|
|
|
DWORD _prot = PAGE_NOACCESS;
|
|
|
|
switch (prot)
|
|
|
|
{
|
|
|
|
case protection::rw: _prot = PAGE_READWRITE; break;
|
|
|
|
case protection::ro: _prot = PAGE_READONLY; break;
|
|
|
|
case protection::no: break;
|
|
|
|
case protection::wx: _prot = PAGE_EXECUTE_READWRITE; break;
|
|
|
|
case protection::rx: _prot = PAGE_EXECUTE_READ; break;
|
|
|
|
}
|
2015-08-26 15:18:01 +00:00
|
|
|
#else
|
2017-03-19 12:50:56 +00:00
|
|
|
int _prot = PROT_NONE;
|
|
|
|
switch (prot)
|
|
|
|
{
|
|
|
|
case protection::rw: _prot = PROT_READ | PROT_WRITE; break;
|
|
|
|
case protection::ro: _prot = PROT_READ; break;
|
|
|
|
case protection::no: break;
|
|
|
|
case protection::wx: _prot = PROT_READ | PROT_WRITE | PROT_EXEC; break;
|
|
|
|
case protection::rx: _prot = PROT_READ | PROT_EXEC; break;
|
|
|
|
}
|
2015-08-26 15:18:01 +00:00
|
|
|
#endif
|
2017-03-19 12:50:56 +00:00
|
|
|
|
|
|
|
return _prot;
|
2015-08-26 15:18:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-19 12:50:56 +00:00
|
|
|
void* memory_reserve(std::size_t size, void* use_addr)
|
2015-08-26 15:18:01 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2017-03-25 15:56:09 +00:00
|
|
|
return ::VirtualAlloc(use_addr, size, MEM_RESERVE, PAGE_NOACCESS);
|
2015-08-26 15:18:01 +00:00
|
|
|
#else
|
2017-03-25 15:56:09 +00:00
|
|
|
auto ptr = ::mmap(use_addr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
|
|
|
|
|
|
|
|
if (use_addr && ptr != use_addr)
|
|
|
|
{
|
|
|
|
::munmap(ptr, size);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
2017-03-19 12:50:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void memory_commit(void* pointer, std::size_t size, protection prot)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
verify(HERE), ::VirtualAlloc(pointer, size, MEM_COMMIT, +prot);
|
|
|
|
#else
|
2019-11-28 18:18:37 +00:00
|
|
|
const u64 ptr64 = reinterpret_cast<u64>(pointer);
|
|
|
|
verify(HERE), ::mprotect(reinterpret_cast<void*>(ptr64 & -4096), size + (ptr64 & 4095), +prot) != -1;
|
2015-08-26 15:18:01 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-03-13 09:40:54 +00:00
|
|
|
void memory_decommit(void* pointer, std::size_t size)
|
2015-08-26 15:18:01 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2017-10-05 21:24:50 +00:00
|
|
|
verify(HERE), ::VirtualFree(pointer, size, MEM_DECOMMIT);
|
2015-08-26 15:18:01 +00:00
|
|
|
#else
|
2017-02-10 19:56:16 +00:00
|
|
|
verify(HERE), ::mmap(pointer, size, PROT_NONE, MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
|
2017-03-13 09:40:54 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-09-22 20:14:28 +00:00
|
|
|
void memory_reset(void* pointer, std::size_t size, protection prot)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
memory_decommit(pointer, size);
|
|
|
|
memory_commit(pointer, size, prot);
|
|
|
|
#else
|
|
|
|
verify(HERE), ::mmap(pointer, size, +prot, MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-05-07 18:57:06 +00:00
|
|
|
void memory_release(void* pointer, std::size_t size)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
verify(HERE), ::VirtualFree(pointer, 0, MEM_RELEASE);
|
|
|
|
#else
|
|
|
|
verify(HERE), ::munmap(pointer, size) != -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-03-13 09:40:54 +00:00
|
|
|
void memory_protect(void* pointer, std::size_t size, protection prot)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2019-11-28 18:18:37 +00:00
|
|
|
for (u64 addr = reinterpret_cast<u64>(pointer), end = addr + size; addr < end;)
|
2018-05-07 18:57:06 +00:00
|
|
|
{
|
2018-07-14 11:51:41 +00:00
|
|
|
const u64 boundary = (addr + 0x10000) & -0x10000;
|
|
|
|
const u64 block_size = std::min(boundary, end) - addr;
|
2018-05-07 18:57:06 +00:00
|
|
|
|
|
|
|
DWORD old;
|
2019-11-28 18:18:37 +00:00
|
|
|
if (!::VirtualProtect(reinterpret_cast<LPVOID>(addr), block_size, +prot, &old))
|
2018-05-07 18:57:06 +00:00
|
|
|
{
|
|
|
|
fmt::throw_exception("VirtualProtect failed (%p, 0x%x, addr=0x%x, error=%#x)", pointer, size, addr, GetLastError());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next region
|
2018-07-14 11:51:41 +00:00
|
|
|
addr += block_size;
|
2018-05-07 18:57:06 +00:00
|
|
|
}
|
2017-03-13 09:40:54 +00:00
|
|
|
#else
|
2019-11-28 18:18:37 +00:00
|
|
|
const u64 ptr64 = reinterpret_cast<u64>(pointer);
|
|
|
|
verify(HERE), ::mprotect(reinterpret_cast<void*>(ptr64 & -4096), size + (ptr64 & 4095), +prot) != -1;
|
2018-05-07 18:57:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
shm::shm(u32 size)
|
|
|
|
: m_size(::align(size, 0x10000))
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
m_handle = ::CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_EXECUTE_READWRITE, 0, m_size, NULL);
|
2018-05-13 19:50:38 +00:00
|
|
|
verify(HERE), m_handle != INVALID_HANDLE_VALUE;
|
2018-05-27 10:30:55 +00:00
|
|
|
#elif __linux__
|
|
|
|
m_file = ::memfd_create_("", 0);
|
|
|
|
verify(HERE), m_file >= 0;
|
|
|
|
verify(HERE), ::ftruncate(m_file, m_size) >= 0;
|
2018-05-07 18:57:06 +00:00
|
|
|
#else
|
|
|
|
while ((m_file = ::shm_open("/rpcs3-mem1", O_RDWR | O_CREAT | O_EXCL, S_IWUSR | S_IRUSR)) == -1)
|
|
|
|
{
|
2018-05-13 19:50:38 +00:00
|
|
|
if (m_file == -1 && errno == EMFILE)
|
|
|
|
{
|
|
|
|
fmt::throw_exception("Too many open files. Raise the limit and try again.");
|
|
|
|
}
|
|
|
|
|
|
|
|
verify(HERE), errno == EEXIST;
|
2018-05-07 18:57:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 19:50:38 +00:00
|
|
|
verify(HERE), ::shm_unlink("/rpcs3-mem1") >= 0;
|
|
|
|
verify(HERE), ::ftruncate(m_file, m_size) >= 0;
|
2018-05-07 18:57:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
shm::~shm()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
::CloseHandle(m_handle);
|
|
|
|
#else
|
|
|
|
::close(m_file);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
u8* shm::map(void* ptr, protection prot) const
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2018-08-27 14:52:47 +00:00
|
|
|
DWORD access = FILE_MAP_WRITE;
|
2018-05-07 18:57:06 +00:00
|
|
|
switch (prot)
|
|
|
|
{
|
2018-08-27 14:52:47 +00:00
|
|
|
case protection::rw:
|
|
|
|
case protection::ro:
|
|
|
|
case protection::no:
|
|
|
|
break;
|
|
|
|
case protection::wx:
|
|
|
|
case protection::rx:
|
|
|
|
access |= FILE_MAP_EXECUTE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto ret = static_cast<u8*>(::MapViewOfFileEx(m_handle, access, 0, 0, m_size, ptr)))
|
|
|
|
{
|
|
|
|
if (prot != protection::rw && prot != protection::wx)
|
|
|
|
{
|
|
|
|
DWORD old;
|
|
|
|
if (!::VirtualProtect(ret, m_size, +prot, &old))
|
|
|
|
{
|
|
|
|
::UnmapViewOfFile(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2018-05-07 18:57:06 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 14:52:47 +00:00
|
|
|
return nullptr;
|
2018-05-07 18:57:06 +00:00
|
|
|
#else
|
2019-11-28 18:18:37 +00:00
|
|
|
const u64 ptr64 = reinterpret_cast<u64>(ptr);
|
|
|
|
return static_cast<u8*>(::mmap(reinterpret_cast<void*>(ptr64 & -0x10000), m_size, +prot, MAP_SHARED | (ptr ? MAP_FIXED : 0), m_file, 0));
|
2018-05-07 18:57:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
u8* shm::map_critical(void* ptr, protection prot)
|
|
|
|
{
|
2019-11-28 18:18:37 +00:00
|
|
|
const auto target = reinterpret_cast<u8*>(reinterpret_cast<u64>(ptr) & -0x10000);
|
2018-05-07 18:57:06 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
::MEMORY_BASIC_INFORMATION mem;
|
|
|
|
if (!::VirtualQuery(target, &mem, sizeof(mem)) || mem.State != MEM_RESERVE || !::VirtualFree(mem.AllocationBase, 0, MEM_RELEASE))
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto base = (u8*)mem.AllocationBase;
|
|
|
|
const auto size = mem.RegionSize + (target - base);
|
|
|
|
|
|
|
|
if (base < target && !::VirtualAlloc(base, target - base, MEM_RESERVE, PAGE_NOACCESS))
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target + m_size < base + size && !::VirtualAlloc(target + m_size, base + size - target - m_size, MEM_RESERVE, PAGE_NOACCESS))
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return this->map(target, prot);
|
|
|
|
}
|
|
|
|
|
|
|
|
void shm::unmap(void* ptr) const
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
::UnmapViewOfFile(ptr);
|
|
|
|
#else
|
|
|
|
::munmap(ptr, m_size);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void shm::unmap_critical(void* ptr)
|
|
|
|
{
|
2019-11-28 18:18:37 +00:00
|
|
|
const auto target = reinterpret_cast<u8*>(reinterpret_cast<u64>(ptr) & -0x10000);
|
2018-05-07 18:57:06 +00:00
|
|
|
|
|
|
|
this->unmap(target);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
::MEMORY_BASIC_INFORMATION mem, mem2;
|
|
|
|
if (!::VirtualQuery(target - 1, &mem, sizeof(mem)) || !::VirtualQuery(target + m_size, &mem2, sizeof(mem2)))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mem.State == MEM_RESERVE && !::VirtualFree(mem.AllocationBase, 0, MEM_RELEASE))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mem2.State == MEM_RESERVE && !::VirtualFree(mem2.AllocationBase, 0, MEM_RELEASE))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto size1 = mem.State == MEM_RESERVE ? target - (u8*)mem.AllocationBase : 0;
|
|
|
|
const auto size2 = mem2.State == MEM_RESERVE ? mem2.RegionSize : 0;
|
|
|
|
|
|
|
|
if (!::VirtualAlloc(mem.State == MEM_RESERVE ? mem.AllocationBase : target, m_size + size1 + size2, MEM_RESERVE, PAGE_NOACCESS))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-08-26 15:18:01 +00:00
|
|
|
#endif
|
|
|
|
}
|
2015-09-26 20:46:04 +00:00
|
|
|
}
|