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
|
|
|
|
2010-01-22 15:22:01 +00:00
|
|
|
virtual void DispatchEvent( const PageFaultInfo& evtinfo )
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
protected:
|
2010-01-22 15:22:01 +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
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// EventListener_PageFault
|
|
|
|
// --------------------------------------------------------------------------------------
|
2010-01-23 17:13:03 +00:00
|
|
|
class EventListener_PageFault : public IEventListener_PageFault
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
EventListener_PageFault();
|
|
|
|
virtual ~EventListener_PageFault() throw();
|
|
|
|
};
|
|
|
|
|
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:
|
|
|
|
SrcType_PageFault() {}
|
|
|
|
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-10-22 16:23:52 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// BaseVirtualMemoryReserve (WIP!!)
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
class BaseVirtualMemoryReserve : public EventListener_PageFault
|
|
|
|
{
|
|
|
|
DeclareNoncopyableObject( BaseVirtualMemoryReserve );
|
|
|
|
|
|
|
|
public:
|
|
|
|
wxString Name;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void* m_baseptr;
|
|
|
|
|
|
|
|
// reserved memory (in pages).
|
|
|
|
uptr m_reserved;
|
|
|
|
|
|
|
|
// Incremental size by which the buffer grows (in pages)
|
2010-11-03 17:37:29 +00:00
|
|
|
uptr m_blocksize;
|
2010-10-22 16:23:52 +00:00
|
|
|
|
|
|
|
// Protection mode to be applied to committed blocks.
|
|
|
|
PageProtectionMode m_prot_mode;
|
|
|
|
|
|
|
|
// Records the number of pages committed to memory.
|
|
|
|
// (metric for analysis of buffer usage)
|
|
|
|
uptr m_commited;
|
|
|
|
|
|
|
|
public:
|
|
|
|
BaseVirtualMemoryReserve( const wxString& name );
|
|
|
|
virtual ~BaseVirtualMemoryReserve() throw()
|
|
|
|
{
|
|
|
|
Free();
|
|
|
|
}
|
|
|
|
|
2010-10-27 19:18:46 +00:00
|
|
|
virtual void* Reserve( uint size, uptr base = 0, uptr upper_bounds = 0 );
|
|
|
|
virtual void Reset();
|
|
|
|
virtual void Free();
|
2010-11-03 17:37:29 +00:00
|
|
|
virtual bool TryResize( uint newsize );
|
|
|
|
|
|
|
|
virtual void CommitBlocks( uptr page, uint blocks );
|
2010-10-27 19:18:46 +00:00
|
|
|
|
|
|
|
bool IsOk() const { return m_baseptr != NULL; }
|
2010-10-29 02:49:01 +00:00
|
|
|
wxString GetName() const { return Name; }
|
|
|
|
|
2010-10-22 16:23:52 +00:00
|
|
|
uptr GetReserveSizeInBytes() const { return m_reserved * __pagesize; }
|
|
|
|
uptr GetReserveSizeInPages() const { return m_reserved; }
|
|
|
|
|
|
|
|
u8* GetPtr() { return (u8*)m_baseptr; }
|
|
|
|
const u8* GetPtr() const { return (u8*)m_baseptr; }
|
|
|
|
|
|
|
|
u8* GetPtrEnd() { return (u8*)m_baseptr + (m_reserved * __pagesize); }
|
|
|
|
const u8* GetPtrEnd() const { return (u8*)m_baseptr + (m_reserved * __pagesize); }
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
pxAssume(idx < (m_reserved * __pagesize));
|
|
|
|
return *((u8*)m_baseptr + idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
const u8& operator[](uint idx) const
|
|
|
|
{
|
|
|
|
pxAssume(idx < (m_reserved * __pagesize));
|
|
|
|
return *((u8*)m_baseptr + idx);
|
|
|
|
}
|
|
|
|
|
2010-10-22 16:23:52 +00:00
|
|
|
protected:
|
|
|
|
void OnPageFaultEvent( const PageFaultInfo& info, bool& handled );
|
|
|
|
|
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-10-27 19:18:46 +00:00
|
|
|
// SpatialArrayReserve
|
2010-10-22 16:23:52 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
2010-10-27 19:18:46 +00:00
|
|
|
// A spatial array is one where large areas of the memory reserve will remain unused during
|
|
|
|
// process execution. Only areas put to use will be committed to virtual memory.
|
|
|
|
//
|
|
|
|
// Spatial array efficiency depends heavily on selecting the right parameters for the array's
|
|
|
|
// primary intended use. Memory in a spatial array is arranged by blocks, with each block
|
|
|
|
// containing some number of pages (pages are 4096 bytes each on most platforms). When the
|
|
|
|
// array is accessed, the entire block containing the addressed memory will be committed at
|
|
|
|
// once. Blocks can be a single page in size (4096 bytes), though this is highly discouraged
|
|
|
|
// due to overhead and fragmentation penalties.
|
|
|
|
//
|
|
|
|
// Balancing block sizes:
|
|
|
|
// Larger blocks are good for reducing memory fragmentation and block-tracking overhead, but
|
|
|
|
// can also result in a lot of otherwise unused memory being committed to memory. Smaller
|
|
|
|
// blocks are good for arrays that will tend toward more sequential behavior, as they reduce
|
|
|
|
// the amount of unused memory being committed. However, since every block requires a
|
|
|
|
// tracking entry, assigning small blocks to a very large array can result in quite a bit of
|
|
|
|
// unwanted overhead. Furthermore, if the array is accessed randomly, system physical memory
|
|
|
|
// will become very fragmented, which will also hurt performance.
|
|
|
|
//
|
|
|
|
// By default, the base block size is based on a heuristic that balances the size of the spatial
|
|
|
|
// array reserve against a best-guess performance profile for the target platform.
|
|
|
|
//
|
|
|
|
class SpatialArrayReserve : public BaseVirtualMemoryReserve
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
2010-10-27 19:18:46 +00:00
|
|
|
typedef BaseVirtualMemoryReserve __parent;
|
|
|
|
|
2010-10-22 16:23:52 +00:00
|
|
|
protected:
|
2010-11-03 17:37:29 +00:00
|
|
|
uint m_numblocks;
|
|
|
|
|
|
|
|
// Array of block bits, each bit indicating if the block has been committed to memory
|
|
|
|
// or not. The array length is typically determined via ((numblocks+7) / 8), though the
|
|
|
|
// actual array size may be larger in order to accommodate 32-bit or 128-bit accelerated
|
|
|
|
// operations.
|
|
|
|
ScopedAlignedAlloc<u8,16> m_blockbits;
|
2010-10-22 16:23:52 +00:00
|
|
|
|
|
|
|
public:
|
2010-11-06 16:25:40 +00:00
|
|
|
SpatialArrayReserve( const wxString& name );
|
2010-10-27 19:18:46 +00:00
|
|
|
|
|
|
|
virtual void* Reserve( uint size, uptr base = 0, uptr upper_bounds = 0 );
|
2010-11-03 17:37:29 +00:00
|
|
|
virtual void Reset();
|
2010-11-05 01:33:01 +00:00
|
|
|
virtual bool TryResize( uint newsize );
|
2010-10-27 19:18:46 +00:00
|
|
|
|
2010-10-22 16:23:52 +00:00
|
|
|
void OnCommittedBlock( void* block );
|
2010-10-27 19:18:46 +00:00
|
|
|
|
|
|
|
SpatialArrayReserve& SetBlockCount( uint blocks );
|
|
|
|
SpatialArrayReserve& SetBlockSizeInPages( uint bytes );
|
|
|
|
uint SetBlockSize( uint bytes );
|
|
|
|
|
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
|
|
|
|
|
|
|
using __parent::operator[];
|
2010-11-03 17:37:29 +00:00
|
|
|
|
|
|
|
protected:
|
2010-11-06 16:25:40 +00:00
|
|
|
void DoCommitAndProtect( uptr page );
|
2010-11-03 17:37:29 +00:00
|
|
|
uint _calcBlockBitArrayLength() const;
|
2010-10-22 16:23:52 +00:00
|
|
|
};
|
|
|
|
|
2009-10-23 20:24:59 +00:00
|
|
|
#ifdef __LINUX__
|
|
|
|
|
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
|
|
|
|
|
|
|
# define PCSX2_PAGEFAULT_PROTECT __try
|
|
|
|
# define PCSX2_PAGEFAULT_EXCEPT __except(SysPageFaultExceptionFilter(GetExceptionInformation())) {}
|
|
|
|
|
|
|
|
#else
|
|
|
|
# error PCSX2 - Unsupported operating system platform.
|
|
|
|
#endif
|
2010-01-22 15:22:01 +00:00
|
|
|
|
|
|
|
extern void InstallSignalHandler();
|
|
|
|
|
2010-10-22 19:47:02 +00:00
|
|
|
extern SrcType_PageFault* Source_PageFault;
|
2010-10-22 16:23:52 +00:00
|
|
|
|