2009-10-23 20:24:59 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
2010-05-03 14:08:02 +00:00
|
|
|
* Copyright (C) 2002-2010 PCSX2 Dev Team
|
2009-10-23 20:24:59 +00:00
|
|
|
*
|
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-10-27 19:18:46 +00:00
|
|
|
// [TODO] Rename this file to VirtualMemory.h !!
|
|
|
|
|
2009-10-23 20:24:59 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// =====================================================================================================
|
|
|
|
// Cross-Platform Memory Protection (Used by VTLB, Recompilers and Texture caches)
|
|
|
|
// =====================================================================================================
|
|
|
|
// Win32 platforms use the SEH model: __try {} __except {}
|
|
|
|
// Linux platforms use the POSIX Signals model: sigaction()
|
2010-10-27 19:18:46 +00:00
|
|
|
// [TODO] OS-X (Darwin) platforms should use the Mach exception model (not implemented)
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2010-10-27 19:18:46 +00:00
|
|
|
#include "EventSource.h"
|
2021-05-13 12:09:28 +00:00
|
|
|
#include "General.h"
|
2022-05-09 10:11:30 +00:00
|
|
|
#include "Assertions.h"
|
2020-08-19 08:20:48 +00:00
|
|
|
#include <atomic>
|
2022-05-18 13:27:23 +00:00
|
|
|
#include <memory>
|
2022-05-09 10:11:30 +00:00
|
|
|
#include <mutex>
|
2022-05-18 13:27:23 +00:00
|
|
|
#include <string>
|
2009-10-23 20:24:59 +00:00
|
|
|
|
|
|
|
struct PageFaultInfo
|
|
|
|
{
|
2022-10-29 03:39:19 +00:00
|
|
|
uptr pc;
|
2021-09-06 18:28:26 +00:00
|
|
|
uptr addr;
|
2009-10-23 21:05:32 +00:00
|
|
|
|
2022-10-29 03:39:19 +00:00
|
|
|
PageFaultInfo(uptr pc_, uptr address)
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2022-10-29 03:39:19 +00:00
|
|
|
pc = pc_;
|
2021-09-06 18:28:26 +00:00
|
|
|
addr = address;
|
|
|
|
}
|
2009-10-23 20:24:59 +00:00
|
|
|
};
|
|
|
|
|
2010-01-22 15:22:01 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// IEventListener_PageFault
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
class IEventListener_PageFault : public IEventDispatcher<PageFaultInfo>
|
2009-10-23 20:24:59 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-09-06 18:28:26 +00:00
|
|
|
typedef PageFaultInfo EvtParams;
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2010-01-22 15:22:01 +00:00
|
|
|
public:
|
2021-09-06 18:28:26 +00:00
|
|
|
virtual ~IEventListener_PageFault() = default;
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
virtual void DispatchEvent(const PageFaultInfo& evtinfo, bool& handled)
|
|
|
|
{
|
|
|
|
OnPageFaultEvent(evtinfo, handled);
|
|
|
|
}
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
virtual void DispatchEvent(const PageFaultInfo& evtinfo)
|
|
|
|
{
|
|
|
|
pxFailRel("Don't call me, damnit. Use DispatchException instead.");
|
|
|
|
}
|
2010-01-22 15:22:01 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
virtual void OnPageFaultEvent(const PageFaultInfo& evtinfo, bool& handled) {}
|
2009-10-23 20:24:59 +00:00
|
|
|
};
|
|
|
|
|
2010-10-22 16:23:52 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
2010-11-15 14:05:02 +00:00
|
|
|
// EventListener_PageFault / EventListenerHelper_PageFault
|
2010-10-22 16:23:52 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
2010-01-23 17:13:03 +00:00
|
|
|
class EventListener_PageFault : public IEventListener_PageFault
|
|
|
|
{
|
|
|
|
public:
|
2021-09-06 18:28:26 +00:00
|
|
|
EventListener_PageFault();
|
|
|
|
virtual ~EventListener_PageFault();
|
2010-01-23 17:13:03 +00:00
|
|
|
};
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename TypeToDispatchTo>
|
2010-11-15 14:05:02 +00:00
|
|
|
class EventListenerHelper_PageFault : public EventListener_PageFault
|
|
|
|
{
|
|
|
|
public:
|
2021-09-06 18:28:26 +00:00
|
|
|
TypeToDispatchTo* Owner;
|
2010-11-15 14:05:02 +00:00
|
|
|
|
|
|
|
public:
|
2021-09-06 18:28:26 +00:00
|
|
|
EventListenerHelper_PageFault(TypeToDispatchTo& dispatchTo)
|
|
|
|
{
|
|
|
|
Owner = &dispatchTo;
|
|
|
|
}
|
2010-11-15 14:05:02 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
EventListenerHelper_PageFault(TypeToDispatchTo* dispatchTo)
|
|
|
|
{
|
|
|
|
Owner = dispatchTo;
|
|
|
|
}
|
2010-11-15 14:05:02 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
virtual ~EventListenerHelper_PageFault() = default;
|
2010-11-15 14:05:02 +00:00
|
|
|
|
|
|
|
protected:
|
2021-09-06 18:28:26 +00:00
|
|
|
virtual void OnPageFaultEvent(const PageFaultInfo& info, bool& handled)
|
|
|
|
{
|
|
|
|
Owner->OnPageFaultEvent(info, handled);
|
|
|
|
}
|
2010-11-15 14:05:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// SrcType_PageFault
|
|
|
|
// --------------------------------------------------------------------------------------
|
2010-01-22 15:22:01 +00:00
|
|
|
class SrcType_PageFault : public EventSource<IEventListener_PageFault>
|
|
|
|
{
|
|
|
|
protected:
|
2021-09-06 18:28:26 +00:00
|
|
|
typedef EventSource<IEventListener_PageFault> _parent;
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2010-01-22 15:22:01 +00:00
|
|
|
protected:
|
2021-09-06 18:28:26 +00:00
|
|
|
bool m_handled;
|
2010-01-22 15:22:01 +00:00
|
|
|
|
|
|
|
public:
|
2021-09-06 18:28:26 +00:00
|
|
|
SrcType_PageFault()
|
|
|
|
: m_handled(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual ~SrcType_PageFault() = default;
|
2010-01-22 15:22:01 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
bool WasHandled() const { return m_handled; }
|
|
|
|
virtual void Dispatch(const PageFaultInfo& params);
|
2010-01-22 15:22:01 +00:00
|
|
|
|
|
|
|
protected:
|
2021-09-06 18:28:26 +00:00
|
|
|
virtual void _DispatchRaw(ListenerIterator iter, const ListenerIterator& iend, const PageFaultInfo& evt);
|
2010-01-22 15:22:01 +00:00
|
|
|
};
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2010-11-15 14:05:02 +00:00
|
|
|
|
2020-08-19 08:20:48 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// VirtualMemoryManager: Manages the allocation of PCSX2 VM
|
|
|
|
// Ensures that all memory is close enough together for rip-relative addressing
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
class VirtualMemoryManager
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
DeclareNoncopyableObject(VirtualMemoryManager);
|
2020-08-19 08:20:48 +00:00
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
std::string m_name;
|
2020-08-19 08:20:48 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
void* m_file_handle;
|
|
|
|
u8* m_baseptr;
|
2020-08-19 08:20:48 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
// An array to track page usage (to trigger asserts if things try to overlap)
|
|
|
|
std::atomic<bool>* m_pageuse;
|
2020-08-19 08:20:48 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
// reserved memory (in pages)
|
|
|
|
u32 m_pages_reserved;
|
2020-08-19 08:20:48 +00:00
|
|
|
|
|
|
|
public:
|
2021-09-06 18:28:26 +00:00
|
|
|
// If upper_bounds is nonzero and the OS fails to allocate memory that is below it,
|
|
|
|
// calls to IsOk() will return false and Alloc() will always return null pointers
|
|
|
|
// strict indicates that the allocation should quietly fail if the memory can't be mapped at `base`
|
2022-10-12 13:57:53 +00:00
|
|
|
VirtualMemoryManager(std::string name, const char* file_mapping_name, uptr base, size_t size, uptr upper_bounds = 0, bool strict = false);
|
2021-09-06 18:28:26 +00:00
|
|
|
~VirtualMemoryManager();
|
2020-08-19 08:20:48 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
bool IsSharedMemory() const { return (m_file_handle != nullptr); }
|
|
|
|
void* GetFileHandle() const { return m_file_handle; }
|
|
|
|
u8* GetBase() const { return m_baseptr; }
|
|
|
|
u8* GetEnd() const { return (m_baseptr + m_pages_reserved * __pagesize); }
|
2020-08-19 08:20:48 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
// Request the use of the memory at offsetLocation bytes from the start of the reserved memory area
|
|
|
|
// offsetLocation must be page-aligned
|
2022-10-12 13:57:53 +00:00
|
|
|
u8* Alloc(uptr offsetLocation, size_t size) const;
|
2020-08-19 08:20:48 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
u8* AllocAtAddress(void* address, size_t size) const
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2022-10-12 13:57:53 +00:00
|
|
|
return Alloc(size, static_cast<const u8*>(address) - m_baseptr);
|
2021-09-06 18:28:26 +00:00
|
|
|
}
|
2020-08-19 08:20:48 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
void Free(void* address, size_t size) const;
|
2020-08-19 08:20:48 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
// Was this VirtualMemoryManager successfully able to get its memory mapping?
|
|
|
|
// (If not, calls to Alloc will return null pointers)
|
|
|
|
bool IsOk() const { return m_baseptr != 0; }
|
2020-08-19 08:20:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::shared_ptr<const VirtualMemoryManager> VirtualMemoryManagerPtr;
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// VirtualMemoryBumpAllocator: Allocates memory for things that don't have explicitly-reserved spots
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
class VirtualMemoryBumpAllocator
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
const VirtualMemoryManagerPtr m_allocator;
|
2022-10-12 13:57:53 +00:00
|
|
|
std::atomic<u8*> m_baseptr{0};
|
|
|
|
const u8* m_endptr = 0;
|
2021-09-06 18:28:26 +00:00
|
|
|
|
2020-08-19 08:20:48 +00:00
|
|
|
public:
|
2021-09-06 18:28:26 +00:00
|
|
|
VirtualMemoryBumpAllocator(VirtualMemoryManagerPtr allocator, size_t size, uptr offsetLocation);
|
2022-10-12 13:57:53 +00:00
|
|
|
u8* Alloc(size_t size);
|
2021-09-06 18:28:26 +00:00
|
|
|
const VirtualMemoryManagerPtr& GetAllocator() { return m_allocator; }
|
2020-08-19 08:20:48 +00:00
|
|
|
};
|
|
|
|
|
2010-10-22 16:23:52 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
2010-11-15 14:05:02 +00:00
|
|
|
// VirtualMemoryReserve
|
2010-10-22 16:23:52 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
2010-11-15 14:05:02 +00:00
|
|
|
class VirtualMemoryReserve
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
DeclareNoncopyableObject(VirtualMemoryReserve);
|
2010-10-22 16:23:52 +00:00
|
|
|
|
|
|
|
protected:
|
2022-05-18 13:27:23 +00:00
|
|
|
std::string m_name;
|
2016-11-12 15:28:37 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
// Where the memory came from (so we can return it)
|
|
|
|
VirtualMemoryManagerPtr m_allocator;
|
2020-08-19 08:20:48 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
u8* m_baseptr = nullptr;
|
|
|
|
size_t m_size = 0;
|
2020-08-19 08:20:48 +00:00
|
|
|
|
2010-10-22 16:23:52 +00:00
|
|
|
public:
|
2022-10-12 13:57:53 +00:00
|
|
|
VirtualMemoryReserve(std::string name);
|
|
|
|
virtual ~VirtualMemoryReserve();
|
2021-09-06 18:28:26 +00:00
|
|
|
|
|
|
|
// Initialize with the given piece of memory
|
|
|
|
// Note: The memory is already allocated, the allocator is for future use to free the region
|
|
|
|
// It may be null in which case there is no way to free the memory in a way it will be usable again
|
2022-10-12 13:57:53 +00:00
|
|
|
void Assign(VirtualMemoryManagerPtr allocator, u8* baseptr, size_t size);
|
2021-09-06 18:28:26 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
u8* BumpAllocate(VirtualMemoryBumpAllocator& allocator, size_t size);
|
2021-09-06 18:28:26 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
void Release();
|
2021-09-06 18:28:26 +00:00
|
|
|
|
|
|
|
bool IsOk() const { return m_baseptr != NULL; }
|
2022-05-18 13:27:23 +00:00
|
|
|
const std::string& GetName() const { return m_name; }
|
2021-09-06 18:28:26 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
u8* GetPtr() { return m_baseptr; }
|
|
|
|
const u8* GetPtr() const { return m_baseptr; }
|
|
|
|
u8* GetPtrEnd() { return m_baseptr + m_size; }
|
|
|
|
const u8* GetPtrEnd() const { return m_baseptr + m_size; }
|
2021-09-06 18:28:26 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
size_t GetSize() const { return m_size; }
|
2021-09-06 18:28:26 +00:00
|
|
|
|
|
|
|
operator void*() { return m_baseptr; }
|
|
|
|
operator const void*() const { return m_baseptr; }
|
|
|
|
|
|
|
|
operator u8*() { return (u8*)m_baseptr; }
|
|
|
|
operator const u8*() const { return (u8*)m_baseptr; }
|
|
|
|
|
|
|
|
u8& operator[](uint idx)
|
|
|
|
{
|
2022-10-12 13:57:53 +00:00
|
|
|
pxAssert(idx < m_size);
|
2021-09-06 18:28:26 +00:00
|
|
|
return *((u8*)m_baseptr + idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
const u8& operator[](uint idx) const
|
|
|
|
{
|
2022-10-12 13:57:53 +00:00
|
|
|
pxAssert(idx < m_size);
|
2021-09-06 18:28:26 +00:00
|
|
|
return *((u8*)m_baseptr + idx);
|
|
|
|
}
|
2010-11-15 14:05:02 +00:00
|
|
|
};
|
|
|
|
|
2015-11-17 17:35:02 +00:00
|
|
|
#ifdef __POSIX__
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
#define PCSX2_PAGEFAULT_PROTECT
|
|
|
|
#define PCSX2_PAGEFAULT_EXCEPT
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
#elif defined(_WIN32)
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2010-01-22 15:22:01 +00:00
|
|
|
struct _EXCEPTION_POINTERS;
|
2021-09-06 18:28:26 +00:00
|
|
|
extern long __stdcall SysPageFaultExceptionFilter(struct _EXCEPTION_POINTERS* eps);
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
#define PCSX2_PAGEFAULT_PROTECT __try
|
|
|
|
#define PCSX2_PAGEFAULT_EXCEPT \
|
2021-09-06 18:28:26 +00:00
|
|
|
__except (SysPageFaultExceptionFilter(GetExceptionInformation())) {}
|
2009-10-23 20:24:59 +00:00
|
|
|
|
|
|
|
#else
|
2016-11-12 15:28:37 +00:00
|
|
|
#error PCSX2 - Unsupported operating system platform.
|
2009-10-23 20:24:59 +00:00
|
|
|
#endif
|
2010-01-22 15:22:01 +00:00
|
|
|
|
2010-11-15 14:05:02 +00:00
|
|
|
extern void pxInstallSignalHandler();
|
|
|
|
extern void _platform_InstallSignalHandler();
|
2010-01-22 15:22:01 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
extern SrcType_PageFault* Source_PageFault;
|
2022-05-09 10:11:30 +00:00
|
|
|
extern std::mutex PageFault_Mutex;
|