2023-12-22 11:57:49 +00:00
|
|
|
// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0+
|
2009-04-27 02:04:31 +00:00
|
|
|
|
2021-09-02 16:36:36 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
|
2023-07-22 04:12:59 +00:00
|
|
|
#include "common/BitUtils.h"
|
2021-09-01 20:31:46 +00:00
|
|
|
#include "common/RedtapeWindows.h"
|
2022-05-09 10:11:30 +00:00
|
|
|
#include "common/Console.h"
|
2023-12-26 11:25:10 +00:00
|
|
|
#include "common/HostSys.h"
|
2022-05-18 13:27:23 +00:00
|
|
|
#include "common/StringUtil.h"
|
|
|
|
#include "common/AlignedMalloc.h"
|
2022-12-28 06:53:37 +00:00
|
|
|
#include "common/Assertions.h"
|
2009-04-27 02:04:31 +00:00
|
|
|
|
2022-12-28 06:53:37 +00:00
|
|
|
#include "fmt/core.h"
|
2022-10-29 03:39:19 +00:00
|
|
|
#include "fmt/format.h"
|
|
|
|
|
2022-12-28 06:53:37 +00:00
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
static std::recursive_mutex s_exception_handler_mutex;
|
|
|
|
static PageFaultHandler s_exception_handler_callback;
|
|
|
|
static void* s_exception_handler_handle;
|
|
|
|
static bool s_in_exception_handler;
|
|
|
|
|
|
|
|
long __stdcall SysPageFaultExceptionFilter(EXCEPTION_POINTERS* eps)
|
2010-11-15 14:05:02 +00:00
|
|
|
{
|
2022-12-28 06:53:37 +00:00
|
|
|
// Executing the handler concurrently from multiple threads wouldn't go down well.
|
|
|
|
std::unique_lock lock(s_exception_handler_mutex);
|
|
|
|
|
|
|
|
// Prevent recursive exception filtering.
|
|
|
|
if (s_in_exception_handler)
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
|
|
|
|
// Only interested in page faults.
|
2021-09-06 18:28:26 +00:00
|
|
|
if (eps->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
|
2023-12-26 10:25:26 +00:00
|
|
|
#if defined(_M_AMD64)
|
2022-10-29 03:39:19 +00:00
|
|
|
void* const exception_pc = reinterpret_cast<void*>(eps->ContextRecord->Rip);
|
2023-12-26 10:25:26 +00:00
|
|
|
#elif defined(_M_ARM64)
|
|
|
|
void* const exception_pc = reinterpret_cast<void*>(eps->ContextRecord->Pc);
|
|
|
|
#else
|
|
|
|
void* const exception_pc = nullptr;
|
|
|
|
#endif
|
2022-12-28 06:53:37 +00:00
|
|
|
|
|
|
|
const PageFaultInfo pfi{(uptr)exception_pc, (uptr)eps->ExceptionRecord->ExceptionInformation[1]};
|
|
|
|
|
|
|
|
s_in_exception_handler = true;
|
|
|
|
|
|
|
|
const bool handled = s_exception_handler_callback(pfi);
|
|
|
|
|
|
|
|
s_in_exception_handler = false;
|
|
|
|
|
|
|
|
return handled ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
|
2010-11-15 14:05:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 06:53:37 +00:00
|
|
|
bool HostSys::InstallPageFaultHandler(PageFaultHandler handler)
|
2012-04-07 15:35:53 +00:00
|
|
|
{
|
2022-12-28 06:53:37 +00:00
|
|
|
std::unique_lock lock(s_exception_handler_mutex);
|
|
|
|
pxAssertRel(!s_exception_handler_callback, "A page fault handler is already registered.");
|
|
|
|
if (!s_exception_handler_handle)
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2022-12-28 06:53:37 +00:00
|
|
|
s_exception_handler_handle = AddVectoredExceptionHandler(TRUE, SysPageFaultExceptionFilter);
|
|
|
|
if (!s_exception_handler_handle)
|
|
|
|
return false;
|
2021-09-06 18:28:26 +00:00
|
|
|
}
|
2022-12-28 06:53:37 +00:00
|
|
|
|
|
|
|
s_exception_handler_callback = handler;
|
|
|
|
return true;
|
2012-04-07 15:35:53 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 06:53:37 +00:00
|
|
|
void HostSys::RemovePageFaultHandler(PageFaultHandler handler)
|
2010-11-15 14:05:02 +00:00
|
|
|
{
|
2022-12-28 06:53:37 +00:00
|
|
|
std::unique_lock lock(s_exception_handler_mutex);
|
|
|
|
pxAssertRel(!s_exception_handler_callback || s_exception_handler_callback == handler,
|
|
|
|
"Not removing the same handler previously registered.");
|
|
|
|
s_exception_handler_callback = nullptr;
|
2010-11-15 14:05:02 +00:00
|
|
|
|
2022-12-28 06:53:37 +00:00
|
|
|
if (s_exception_handler_handle)
|
|
|
|
{
|
|
|
|
RemoveVectoredExceptionHandler(s_exception_handler_handle);
|
|
|
|
s_exception_handler_handle = {};
|
|
|
|
}
|
|
|
|
}
|
2010-11-15 14:05:02 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
static DWORD ConvertToWinApi(const PageProtectionMode& mode)
|
2010-11-03 14:18:30 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
DWORD winmode = PAGE_NOACCESS;
|
|
|
|
|
|
|
|
// Windows has some really bizarre memory protection enumeration that uses bitwise
|
|
|
|
// numbering (like flags) but is in fact not a flag value. *Someone* from the early
|
|
|
|
// microsoft days wasn't a very good coder, me thinks. --air
|
|
|
|
|
|
|
|
if (mode.CanExecute())
|
|
|
|
{
|
|
|
|
winmode = mode.CanWrite() ? PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ;
|
|
|
|
}
|
|
|
|
else if (mode.CanRead())
|
|
|
|
{
|
|
|
|
winmode = mode.CanWrite() ? PAGE_READWRITE : PAGE_READONLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return winmode;
|
2010-11-03 14:18:30 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
void* HostSys::Mmap(void* base, size_t size, const PageProtectionMode& mode)
|
2009-04-27 02:04:31 +00:00
|
|
|
{
|
2022-10-12 13:57:53 +00:00
|
|
|
if (mode.IsNone())
|
|
|
|
return nullptr;
|
2021-09-06 18:28:26 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
return VirtualAlloc(base, size, MEM_RESERVE | MEM_COMMIT, ConvertToWinApi(mode));
|
2010-10-22 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
void HostSys::Munmap(void* base, size_t size)
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
2022-10-12 13:57:53 +00:00
|
|
|
if (!base)
|
|
|
|
return;
|
2010-11-03 14:18:30 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
VirtualFree((void*)base, 0, MEM_RELEASE);
|
2010-11-03 14:18:30 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
void HostSys::MemProtect(void* baseaddr, size_t size, const PageProtectionMode& mode)
|
2010-11-03 14:18:30 +00:00
|
|
|
{
|
2022-10-12 13:57:53 +00:00
|
|
|
pxAssert((size & (__pagesize - 1)) == 0);
|
|
|
|
|
|
|
|
DWORD OldProtect; // enjoy my uselessness, yo!
|
|
|
|
if (!VirtualProtect(baseaddr, size, ConvertToWinApi(mode), &OldProtect))
|
|
|
|
pxFail("VirtualProtect() failed");
|
2010-11-03 14:18:30 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
std::string HostSys::GetFileMappingName(const char* prefix)
|
2010-11-03 14:18:30 +00:00
|
|
|
{
|
2022-10-12 13:57:53 +00:00
|
|
|
const unsigned pid = GetCurrentProcessId();
|
|
|
|
return fmt::format("{}_{}", prefix, pid);
|
2010-11-03 14:18:30 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
void* HostSys::CreateSharedMemory(const char* name, size_t size)
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
2022-10-12 13:57:53 +00:00
|
|
|
return static_cast<void*>(CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
|
|
|
|
static_cast<DWORD>(size >> 32), static_cast<DWORD>(size), StringUtil::UTF8StringToWideString(name).c_str()));
|
2010-10-22 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
void HostSys::DestroySharedMemory(void* ptr)
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
2022-10-12 13:57:53 +00:00
|
|
|
CloseHandle(static_cast<HANDLE>(ptr));
|
2010-10-22 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
void* HostSys::MapSharedMemory(void* handle, size_t offset, void* baseaddr, size_t size, const PageProtectionMode& mode)
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
2022-10-12 13:57:53 +00:00
|
|
|
void* ret = MapViewOfFileEx(static_cast<HANDLE>(handle), FILE_MAP_READ | FILE_MAP_WRITE,
|
|
|
|
static_cast<DWORD>(offset >> 32), static_cast<DWORD>(offset), size, baseaddr);
|
|
|
|
if (!ret)
|
|
|
|
return nullptr;
|
2021-09-06 18:28:26 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
const DWORD prot = ConvertToWinApi(mode);
|
|
|
|
if (prot != PAGE_READWRITE)
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2022-10-12 13:57:53 +00:00
|
|
|
DWORD old_prot;
|
|
|
|
if (!VirtualProtect(ret, size, prot, &old_prot))
|
|
|
|
pxFail("Failed to protect memory mapping");
|
2021-09-06 18:28:26 +00:00
|
|
|
}
|
2022-10-12 13:57:53 +00:00
|
|
|
return ret;
|
2009-04-27 02:04:31 +00:00
|
|
|
}
|
2022-10-12 13:57:53 +00:00
|
|
|
|
|
|
|
void HostSys::UnmapSharedMemory(void* baseaddr, size_t size)
|
|
|
|
{
|
|
|
|
if (!UnmapViewOfFile(baseaddr))
|
|
|
|
pxFail("Failed to unmap shared memory");
|
|
|
|
}
|
|
|
|
|
2023-12-26 10:25:26 +00:00
|
|
|
#ifdef _M_ARM64
|
|
|
|
|
|
|
|
void HostSys::FlushInstructionCache(void* address, u32 size)
|
|
|
|
{
|
|
|
|
::FlushInstructionCache(GetCurrentProcess(), address, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-10-29 03:39:19 +00:00
|
|
|
SharedMemoryMappingArea::SharedMemoryMappingArea(u8* base_ptr, size_t size, size_t num_pages)
|
|
|
|
: m_base_ptr(base_ptr)
|
|
|
|
, m_size(size)
|
|
|
|
, m_num_pages(num_pages)
|
|
|
|
{
|
|
|
|
m_placeholder_ranges.emplace(0, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedMemoryMappingArea::~SharedMemoryMappingArea()
|
|
|
|
{
|
|
|
|
pxAssertRel(m_num_mappings == 0, "No mappings left");
|
|
|
|
|
|
|
|
// hopefully this will be okay, and we don't need to coalesce all the placeholders...
|
|
|
|
if (!VirtualFreeEx(GetCurrentProcess(), m_base_ptr, 0, MEM_RELEASE))
|
|
|
|
pxFailRel("Failed to release shared memory area");
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedMemoryMappingArea::PlaceholderMap::iterator SharedMemoryMappingArea::FindPlaceholder(size_t offset)
|
|
|
|
{
|
|
|
|
if (m_placeholder_ranges.empty())
|
|
|
|
return m_placeholder_ranges.end();
|
|
|
|
|
|
|
|
// this will give us an iterator equal or after page
|
|
|
|
auto it = m_placeholder_ranges.lower_bound(offset);
|
|
|
|
if (it == m_placeholder_ranges.end())
|
|
|
|
{
|
|
|
|
// check the last page
|
|
|
|
it = (++m_placeholder_ranges.rbegin()).base();
|
|
|
|
}
|
|
|
|
|
|
|
|
// it's the one we found?
|
|
|
|
if (offset >= it->first && offset < it->second)
|
|
|
|
return it;
|
|
|
|
|
|
|
|
// otherwise try the one before
|
|
|
|
if (it == m_placeholder_ranges.begin())
|
|
|
|
return m_placeholder_ranges.end();
|
|
|
|
|
|
|
|
--it;
|
|
|
|
if (offset >= it->first && offset < it->second)
|
|
|
|
return it;
|
|
|
|
else
|
|
|
|
return m_placeholder_ranges.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<SharedMemoryMappingArea> SharedMemoryMappingArea::Create(size_t size)
|
|
|
|
{
|
|
|
|
pxAssertRel(Common::IsAlignedPow2(size, __pagesize), "Size is page aligned");
|
|
|
|
|
|
|
|
void* alloc = VirtualAlloc2(GetCurrentProcess(), nullptr, size, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS, nullptr, 0);
|
|
|
|
if (!alloc)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return std::unique_ptr<SharedMemoryMappingArea>(new SharedMemoryMappingArea(static_cast<u8*>(alloc), size, size / __pagesize));
|
|
|
|
}
|
|
|
|
|
|
|
|
u8* SharedMemoryMappingArea::Map(void* file_handle, size_t file_offset, void* map_base, size_t map_size, const PageProtectionMode& mode)
|
|
|
|
{
|
|
|
|
pxAssert(static_cast<u8*>(map_base) >= m_base_ptr && static_cast<u8*>(map_base) < (m_base_ptr + m_size));
|
|
|
|
|
|
|
|
const size_t map_offset = static_cast<u8*>(map_base) - m_base_ptr;
|
|
|
|
pxAssert(Common::IsAlignedPow2(map_offset, __pagesize));
|
|
|
|
pxAssert(Common::IsAlignedPow2(map_size, __pagesize));
|
|
|
|
|
|
|
|
// should be a placeholder. unless there's some other mapping we didn't free.
|
|
|
|
PlaceholderMap::iterator phit = FindPlaceholder(map_offset);
|
|
|
|
pxAssertMsg(phit != m_placeholder_ranges.end(), "Page we're mapping is a placeholder");
|
|
|
|
pxAssertMsg(map_offset >= phit->first && map_offset < phit->second, "Page is in returned placeholder range");
|
|
|
|
pxAssertMsg((map_offset + map_size) <= phit->second, "Page range is in returned placeholder range");
|
|
|
|
|
|
|
|
// do we need to split to the left? (i.e. is there a placeholder before this range)
|
|
|
|
const size_t old_ph_end = phit->second;
|
|
|
|
if (map_offset != phit->first)
|
|
|
|
{
|
|
|
|
phit->second = map_offset;
|
|
|
|
|
|
|
|
// split it (i.e. left..start and start..end are now separated)
|
|
|
|
if (!VirtualFreeEx(GetCurrentProcess(), OffsetPointer(phit->first),
|
|
|
|
(map_offset - phit->first), MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER))
|
|
|
|
{
|
|
|
|
pxFailRel("Failed to left split placeholder for map");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// start of the placeholder is getting used, we'll split it right below if there's anything left over
|
|
|
|
m_placeholder_ranges.erase(phit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// do we need to split to the right? (i.e. is there a placeholder after this range)
|
|
|
|
if ((map_offset + map_size) != old_ph_end)
|
|
|
|
{
|
|
|
|
// split out end..ph_end
|
|
|
|
m_placeholder_ranges.emplace(map_offset + map_size, old_ph_end);
|
|
|
|
|
|
|
|
if (!VirtualFreeEx(GetCurrentProcess(), OffsetPointer(map_offset), map_size,
|
|
|
|
MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER))
|
|
|
|
{
|
|
|
|
pxFailRel("Failed to right split placeholder for map");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// actually do the mapping, replacing the placeholder on the range
|
|
|
|
if (!MapViewOfFile3(static_cast<HANDLE>(file_handle), GetCurrentProcess(),
|
|
|
|
map_base, file_offset, map_size, MEM_REPLACE_PLACEHOLDER, PAGE_READWRITE, nullptr, 0))
|
|
|
|
{
|
|
|
|
Console.Error("(SharedMemoryMappingArea) MapViewOfFile3() failed: %u", GetLastError());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DWORD prot = ConvertToWinApi(mode);
|
|
|
|
if (prot != PAGE_READWRITE)
|
|
|
|
{
|
|
|
|
DWORD old_prot;
|
|
|
|
if (!VirtualProtect(map_base, map_size, prot, &old_prot))
|
|
|
|
pxFail("Failed to protect memory mapping");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_num_mappings++;
|
|
|
|
return static_cast<u8*>(map_base);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SharedMemoryMappingArea::Unmap(void* map_base, size_t map_size)
|
|
|
|
{
|
|
|
|
pxAssert(static_cast<u8*>(map_base) >= m_base_ptr && static_cast<u8*>(map_base) < (m_base_ptr + m_size));
|
|
|
|
|
|
|
|
const size_t map_offset = static_cast<u8*>(map_base) - m_base_ptr;
|
|
|
|
pxAssert(Common::IsAlignedPow2(map_offset, __pagesize));
|
|
|
|
pxAssert(Common::IsAlignedPow2(map_size, __pagesize));
|
|
|
|
|
|
|
|
// unmap the specified range
|
|
|
|
if (!UnmapViewOfFile2(GetCurrentProcess(), map_base, MEM_PRESERVE_PLACEHOLDER))
|
|
|
|
{
|
|
|
|
Console.Error("(SharedMemoryMappingArea) UnmapViewOfFile2() failed: %u", GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// can we coalesce to the left?
|
|
|
|
PlaceholderMap::iterator left_it = (map_offset > 0) ? FindPlaceholder(map_offset - 1) : m_placeholder_ranges.end();
|
|
|
|
if (left_it != m_placeholder_ranges.end())
|
|
|
|
{
|
|
|
|
// the left placeholder should end at our start
|
|
|
|
pxAssert(map_offset == left_it->second);
|
|
|
|
left_it->second = map_offset + map_size;
|
|
|
|
|
|
|
|
// combine placeholders before and the range we're unmapping, i.e. to the left
|
|
|
|
if (!VirtualFreeEx(GetCurrentProcess(), OffsetPointer(left_it->first),
|
|
|
|
left_it->second - left_it->first, MEM_RELEASE | MEM_COALESCE_PLACEHOLDERS))
|
|
|
|
{
|
|
|
|
pxFail("Failed to coalesce placeholders left for unmap");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// this is a new placeholder
|
|
|
|
left_it = m_placeholder_ranges.emplace(map_offset, map_offset + map_size).first;
|
|
|
|
}
|
|
|
|
|
|
|
|
// can we coalesce to the right?
|
|
|
|
PlaceholderMap::iterator right_it = ((map_offset + map_size) < m_size) ? FindPlaceholder(map_offset + map_size) : m_placeholder_ranges.end();
|
|
|
|
if (right_it != m_placeholder_ranges.end())
|
|
|
|
{
|
|
|
|
// should start at our end
|
|
|
|
pxAssert(right_it->first == (map_offset + map_size));
|
|
|
|
left_it->second = right_it->second;
|
|
|
|
m_placeholder_ranges.erase(right_it);
|
|
|
|
|
|
|
|
// combine our placeholder and the next, i.e. to the right
|
|
|
|
if (!VirtualFreeEx(GetCurrentProcess(), OffsetPointer(left_it->first),
|
|
|
|
left_it->second - left_it->first, MEM_RELEASE | MEM_COALESCE_PLACEHOLDERS))
|
|
|
|
{
|
|
|
|
pxFail("Failed to coalescae placeholders right for unmap");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_num_mappings--;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-02 16:36:36 +00:00
|
|
|
#endif
|