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"
|
2009-10-23 20:24:59 +00:00
|
|
|
|
|
|
|
struct PageFaultInfo
|
|
|
|
{
|
|
|
|
uptr addr;
|
2009-10-23 21:05:32 +00:00
|
|
|
|
2009-10-23 20:24:59 +00:00
|
|
|
PageFaultInfo( uptr address )
|
|
|
|
{
|
|
|
|
addr = address;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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:
|
2010-01-22 15:22:01 +00:00
|
|
|
typedef PageFaultInfo EvtParams;
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2010-01-22 15:22:01 +00:00
|
|
|
public:
|
2010-01-23 17:13:03 +00:00
|
|
|
virtual ~IEventListener_PageFault() throw() {}
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2010-01-22 15:22:01 +00:00
|
|
|
virtual void DispatchEvent( const PageFaultInfo& evtinfo, bool& handled )
|
|
|
|
{
|
|
|
|
OnPageFaultEvent( evtinfo, handled );
|
|
|
|
}
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2014-07-12 11:30:34 +00:00
|
|
|
virtual void DispatchEvent( const PageFaultInfo& evtinfo )
|
2010-01-22 15:22:01 +00:00
|
|
|
{
|
|
|
|
pxFailRel( "Don't call me, damnit. Use DispatchException instead." );
|
2009-10-23 20:24:59 +00:00
|
|
|
}
|
2010-01-22 15:22:01 +00:00
|
|
|
|
2014-07-12 11:30:34 +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:
|
|
|
|
EventListener_PageFault();
|
|
|
|
virtual ~EventListener_PageFault() throw();
|
|
|
|
};
|
|
|
|
|
2010-11-15 14:05:02 +00:00
|
|
|
template< typename TypeToDispatchTo >
|
|
|
|
class EventListenerHelper_PageFault : public EventListener_PageFault
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TypeToDispatchTo* Owner;
|
|
|
|
|
|
|
|
public:
|
|
|
|
EventListenerHelper_PageFault( TypeToDispatchTo& dispatchTo )
|
|
|
|
{
|
|
|
|
Owner = &dispatchTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventListenerHelper_PageFault( TypeToDispatchTo* dispatchTo )
|
|
|
|
{
|
|
|
|
Owner = dispatchTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~EventListenerHelper_PageFault() throw() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void OnPageFaultEvent( const PageFaultInfo& info, bool& handled )
|
|
|
|
{
|
2010-11-16 00:22:18 +00:00
|
|
|
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:
|
|
|
|
typedef EventSource<IEventListener_PageFault> _parent;
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2010-01-22 15:22:01 +00:00
|
|
|
protected:
|
|
|
|
bool m_handled;
|
|
|
|
|
|
|
|
public:
|
2015-09-21 16:00:48 +00:00
|
|
|
SrcType_PageFault() : m_handled(false) {}
|
2010-01-22 15:22:01 +00:00
|
|
|
virtual ~SrcType_PageFault() throw() { }
|
|
|
|
|
|
|
|
bool WasHandled() const { return m_handled; }
|
|
|
|
virtual void Dispatch( const PageFaultInfo& params );
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void _DispatchRaw( ListenerIterator iter, const ListenerIterator& iend, const PageFaultInfo& evt );
|
|
|
|
};
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2010-11-15 14:05:02 +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
|
|
|
{
|
2010-11-15 14:05:02 +00:00
|
|
|
DeclareNoncopyableObject( VirtualMemoryReserve );
|
2010-10-22 16:23:52 +00:00
|
|
|
|
|
|
|
protected:
|
2010-11-16 15:58:42 +00:00
|
|
|
wxString m_name;
|
|
|
|
|
2010-12-30 06:21:07 +00:00
|
|
|
// Default size of the reserve, in bytes. Can be specified when the object is constructed.
|
2010-11-15 14:05:02 +00:00
|
|
|
// Is used as the reserve size when Reserve() is called, unless an override is specified
|
|
|
|
// in the Reserve parameters.
|
|
|
|
size_t m_defsize;
|
|
|
|
|
2010-10-22 16:23:52 +00:00
|
|
|
void* m_baseptr;
|
|
|
|
|
|
|
|
// reserved memory (in pages).
|
2010-11-15 14:05:02 +00:00
|
|
|
uptr m_pages_reserved;
|
2010-10-22 16:23:52 +00:00
|
|
|
|
|
|
|
// Records the number of pages committed to memory.
|
|
|
|
// (metric for analysis of buffer usage)
|
2010-11-15 14:05:02 +00:00
|
|
|
uptr m_pages_commited;
|
2010-10-22 16:23:52 +00:00
|
|
|
|
2010-12-30 06:21:07 +00:00
|
|
|
// Protection mode to be applied to committed blocks.
|
|
|
|
PageProtectionMode m_prot_mode;
|
|
|
|
|
|
|
|
// Controls write access to the entire reserve. When true (the default), the reserve
|
|
|
|
// operates normally. When set to false, all committed blocks are re-protected with
|
|
|
|
// write disabled, and accesses to uncommitted blocks (read or write) will cause a GPF
|
|
|
|
// as well.
|
|
|
|
bool m_allow_writes;
|
|
|
|
|
2010-10-22 16:23:52 +00:00
|
|
|
public:
|
2010-11-16 15:58:42 +00:00
|
|
|
VirtualMemoryReserve( const wxString& name=wxEmptyString, size_t size = 0 );
|
2010-11-15 14:05:02 +00:00
|
|
|
virtual ~VirtualMemoryReserve() throw()
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void* Reserve( size_t size = 0, uptr base = 0, uptr upper_bounds = 0 );
|
|
|
|
virtual void* ReserveAt( uptr base = 0, uptr upper_bounds = 0 )
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
2010-11-15 14:05:02 +00:00
|
|
|
return Reserve(m_defsize, base, upper_bounds);
|
2010-10-22 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
2010-10-27 19:18:46 +00:00
|
|
|
virtual void Reset();
|
2010-11-15 14:05:02 +00:00
|
|
|
virtual void Release();
|
2010-11-03 17:37:29 +00:00
|
|
|
virtual bool TryResize( uint newsize );
|
2010-11-15 14:05:02 +00:00
|
|
|
virtual bool Commit();
|
2010-12-30 06:21:07 +00:00
|
|
|
|
|
|
|
virtual void ForbidModification();
|
|
|
|
virtual void AllowModification();
|
2010-10-27 19:18:46 +00:00
|
|
|
|
|
|
|
bool IsOk() const { return m_baseptr != NULL; }
|
2010-11-16 15:58:42 +00:00
|
|
|
wxString GetName() const { return m_name; }
|
2010-10-29 02:49:01 +00:00
|
|
|
|
2010-11-15 14:05:02 +00:00
|
|
|
uptr GetReserveSizeInBytes() const { return m_pages_reserved * __pagesize; }
|
|
|
|
uptr GetReserveSizeInPages() const { return m_pages_reserved; }
|
|
|
|
uint GetCommittedPageCount() const { return m_pages_commited; }
|
|
|
|
uint GetCommittedBytes() const { return m_pages_commited * __pagesize; }
|
2010-10-22 16:23:52 +00:00
|
|
|
|
|
|
|
u8* GetPtr() { return (u8*)m_baseptr; }
|
|
|
|
const u8* GetPtr() const { return (u8*)m_baseptr; }
|
2010-11-15 14:05:02 +00:00
|
|
|
u8* GetPtrEnd() { return (u8*)m_baseptr + (m_pages_reserved * __pagesize); }
|
|
|
|
const u8* GetPtrEnd() const { return (u8*)m_baseptr + (m_pages_reserved * __pagesize); }
|
2010-10-22 16:23:52 +00:00
|
|
|
|
2010-11-16 15:58:42 +00:00
|
|
|
VirtualMemoryReserve& SetName( const wxString& newname );
|
2010-11-15 14:05:02 +00:00
|
|
|
VirtualMemoryReserve& SetBaseAddr( uptr newaddr );
|
|
|
|
VirtualMemoryReserve& SetPageAccessOnCommit( const PageProtectionMode& mode );
|
2010-10-22 16:23:52 +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; }
|
|
|
|
|
2010-11-06 16:25:40 +00:00
|
|
|
u8& operator[](uint idx)
|
|
|
|
{
|
2011-07-24 13:02:50 +00:00
|
|
|
pxAssert(idx < (m_pages_reserved * __pagesize));
|
2010-11-06 16:25:40 +00:00
|
|
|
return *((u8*)m_baseptr + idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
const u8& operator[](uint idx) const
|
|
|
|
{
|
2011-07-24 13:02:50 +00:00
|
|
|
pxAssert(idx < (m_pages_reserved * __pagesize));
|
2010-11-06 16:25:40 +00:00
|
|
|
return *((u8*)m_baseptr + idx);
|
|
|
|
}
|
|
|
|
|
2010-12-30 06:21:07 +00:00
|
|
|
protected:
|
|
|
|
virtual void ReprotectCommittedBlocks( const PageProtectionMode& newmode );
|
2010-11-15 14:05:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// BaseVmReserveListener
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
class BaseVmReserveListener : public VirtualMemoryReserve
|
|
|
|
{
|
|
|
|
DeclareNoncopyableObject( BaseVmReserveListener );
|
|
|
|
|
|
|
|
typedef VirtualMemoryReserve _parent;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
EventListenerHelper_PageFault<BaseVmReserveListener> m_pagefault_listener;
|
|
|
|
|
|
|
|
// Incremental size by which the buffer grows (in pages)
|
|
|
|
uptr m_blocksize;
|
|
|
|
|
|
|
|
public:
|
|
|
|
BaseVmReserveListener( const wxString& name, size_t size = 0 );
|
|
|
|
virtual ~BaseVmReserveListener() throw() { }
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
|
|
|
using _parent::operator[];
|
|
|
|
|
2010-10-22 16:23:52 +00:00
|
|
|
void OnPageFaultEvent( const PageFaultInfo& info, bool& handled );
|
|
|
|
|
2010-12-30 06:21:07 +00:00
|
|
|
virtual uptr SetBlockSize( uptr bytes )
|
|
|
|
{
|
|
|
|
m_blocksize = (bytes + __pagesize - 1) / __pagesize;
|
|
|
|
return m_blocksize * __pagesize;
|
|
|
|
}
|
2015-11-11 14:28:56 +00:00
|
|
|
|
|
|
|
virtual void Reset()
|
|
|
|
{
|
|
|
|
_parent::Reset();
|
|
|
|
}
|
2010-12-30 06:21:07 +00:00
|
|
|
|
2010-11-16 00:22:18 +00:00
|
|
|
protected:
|
|
|
|
|
2010-11-03 17:37:29 +00:00
|
|
|
// This function is called from OnPageFaultEvent after the address has been translated
|
|
|
|
// and confirmed to apply to this reserved area in question. OnPageFaultEvent contains
|
|
|
|
// a try/catch exception handler, which ensures "reasonable" error response behavior if
|
|
|
|
// this function throws exceptions.
|
|
|
|
//
|
|
|
|
// Important: This method is called from the context of an exception/signal handler. On
|
|
|
|
// Windows this isn't a big deal (most operations are ok). On Linux, however, logging
|
|
|
|
// and other facilities are probably not a good idea.
|
|
|
|
virtual void DoCommitAndProtect( uptr offset )=0;
|
|
|
|
|
|
|
|
// This function is called for every committed block.
|
2010-10-22 16:23:52 +00:00
|
|
|
virtual void OnCommittedBlock( void* block )=0;
|
2010-11-15 14:05:02 +00:00
|
|
|
|
|
|
|
virtual void CommitBlocks( uptr page, uint blocks );
|
2010-10-22 16:23:52 +00:00
|
|
|
};
|
|
|
|
|
2014-08-03 18:11:22 +00:00
|
|
|
#ifdef __linux__
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2009-10-23 21:05:32 +00:00
|
|
|
# define PCSX2_PAGEFAULT_PROTECT
|
|
|
|
# define PCSX2_PAGEFAULT_EXCEPT
|
2009-10-23 20:24:59 +00:00
|
|
|
|
|
|
|
#elif defined( _WIN32 )
|
|
|
|
|
2010-01-22 15:22:01 +00:00
|
|
|
struct _EXCEPTION_POINTERS;
|
|
|
|
extern int SysPageFaultExceptionFilter(struct _EXCEPTION_POINTERS* eps);
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2011-08-12 02:31:49 +00:00
|
|
|
# define PCSX2_PAGEFAULT_PROTECT __try
|
|
|
|
# define PCSX2_PAGEFAULT_EXCEPT __except(SysPageFaultExceptionFilter(GetExceptionInformation())) {}
|
2009-10-23 20:24:59 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
# error PCSX2 - Unsupported operating system platform.
|
|
|
|
#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
|
|
|
|
2011-08-12 02:31:49 +00:00
|
|
|
#include "Threading.h"
|
2010-10-22 19:47:02 +00:00
|
|
|
extern SrcType_PageFault* Source_PageFault;
|
2011-08-12 02:31:49 +00:00
|
|
|
extern Threading::Mutex PageFault_Mutex;
|
2010-10-22 16:23:52 +00:00
|
|
|
|