Core: Check recompiler memory based on the function size

This commit is contained in:
zilmar 2024-10-17 15:05:48 +10:30
parent 45e52e1d2a
commit f708e5c0b2
5 changed files with 35 additions and 19 deletions

View File

@ -892,25 +892,28 @@ bool CCodeBlock::Compile()
return true; return true;
} }
uint32_t CCodeBlock::Finilize(uint8_t * CompiledLocation) uint32_t CCodeBlock::Finilize(CRecompMemory & RecompMem)
{ {
m_CompiledLocation = RecompMem.RecompPos();
m_CodeHolder.relocateToBase((uint64_t)m_CompiledLocation);
if (CDebugSettings::bRecordRecompilerAsm()) if (CDebugSettings::bRecordRecompilerAsm())
{ {
std::string CodeLog = m_CodeLog; std::string CodeLog = m_CodeLog;
m_CodeLog.clear(); m_CodeLog.clear();
Log("====== Code block ======"); Log("====== Code block ======");
Log("Native entry point: %X", CompiledLocation); Log("Native entry point: %X", m_CompiledLocation);
Log("Start of block: %X", VAddrEnter()); Log("Start of block: %X", VAddrEnter());
Log("Number of sections: %d", NoOfSections()); Log("Number of sections: %d", NoOfSections());
Log("====== Recompiled code ======"); Log("====== Recompiled code ======");
m_CodeLog += CodeLog; m_CodeLog += CodeLog;
} }
m_CompiledLocation = CompiledLocation;
m_CodeHolder.relocateToBase((uint64_t)m_CompiledLocation);
size_t codeSize = m_CodeHolder.codeSize(); size_t codeSize = m_CodeHolder.codeSize();
if (!RecompMem.CheckRecompMem(codeSize))
{
return 0;
}
m_CodeHolder.copyFlattenedData(m_CompiledLocation, codeSize, asmjit::CopySectionFlags::kPadSectionBuffer); m_CodeHolder.copyFlattenedData(m_CompiledLocation, codeSize, asmjit::CopySectionFlags::kPadSectionBuffer);
*m_Recompiler.RecompPos() += codeSize; m_Recompiler.RecompPos() += codeSize;
#if defined(ANDROID) && (defined(__arm__) || defined(_M_ARM)) #if defined(ANDROID) && (defined(__arm__) || defined(_M_ARM))
__clear_cache((uint8_t *)((uint32_t)m_CompiledLocation & ~1), m_CompiledLocation + codeSize); __clear_cache((uint8_t *)((uint32_t)m_CompiledLocation & ~1), m_CompiledLocation + codeSize);

View File

@ -9,6 +9,7 @@
class CMipsMemoryVM; class CMipsMemoryVM;
class CRecompiler; class CRecompiler;
class CRecompMemory;
class CCodeBlock : class CCodeBlock :
public asmjit::ErrorHandler public asmjit::ErrorHandler
@ -18,7 +19,7 @@ public:
~CCodeBlock(); ~CCodeBlock();
bool Compile(); bool Compile();
uint32_t Finilize(uint8_t * CompiledLocation); uint32_t Finilize(CRecompMemory & RecompMem);
asmjit::CodeHolder & CodeHolder(void) asmjit::CodeHolder & CodeHolder(void)
{ {

View File

@ -356,7 +356,6 @@ CCompiledFunc * CRecompiler::CompileCode()
} }
} }
CheckRecompMem();
WriteTrace(TraceRecompiler, TraceDebug, "Compile Block-Start: Program Counter: %016llX pAddr: %X", PROGRAM_COUNTER, pAddr); WriteTrace(TraceRecompiler, TraceDebug, "Compile Block-Start: Program Counter: %016llX pAddr: %X", PROGRAM_COUNTER, pAddr);
CCodeBlock CodeBlock(m_System, (uint32_t)PROGRAM_COUNTER); CCodeBlock CodeBlock(m_System, (uint32_t)PROGRAM_COUNTER);
@ -364,16 +363,19 @@ CCompiledFunc * CRecompiler::CompileCode()
{ {
return nullptr; return nullptr;
} }
uint32_t CodeLen = CodeBlock.Finilize(*this);
if (CodeLen == 0)
{
return nullptr;
}
RecompPos() += CodeLen;
LogCodeBlock(CodeBlock);
if (bShowRecompMemSize()) if (bShowRecompMemSize())
{ {
ShowMemUsed(); ShowMemUsed();
} }
uint32_t CodeLen = CodeBlock.Finilize(*RecompPos());
*RecompPos() += CodeLen;
LogCodeBlock(CodeBlock);
if (bSMM_StoreInstruc()) if (bSMM_StoreInstruc())
{ {
m_MMU.ClearMemoryWriteMap(CodeBlock.VAddrEnter() & ~0xFFF, 0xFFF); m_MMU.ClearMemoryWriteMap(CodeBlock.VAddrEnter() & ~0xFFF, 0xFFF);

View File

@ -49,17 +49,25 @@ bool CRecompMemory::AllocateMemory()
return true; return true;
} }
void CRecompMemory::CheckRecompMem() bool CRecompMemory::CheckRecompMem(uint32_t BlockSize)
{ {
uint32_t Size = (uint32_t)((uint8_t *)m_RecompPos - (uint8_t *)m_RecompCode); uint32_t Size = (uint32_t)((uint8_t *)m_RecompPos - (uint8_t *)m_RecompCode);
if ((Size + 0x50000) < m_RecompSize) if (BlockSize < 0x50000)
{ {
return; BlockSize = 0x50000;
}
if ((Size + BlockSize) < m_RecompSize)
{
return true;
} }
if (m_RecompSize == MaxCompileBufferSize) if (m_RecompSize == MaxCompileBufferSize)
{ {
g_Recompiler->ResetRecompCode(true); g_Recompiler->ResetRecompCode(true);
return; return false;
}
if (BlockSize > IncreaseCompileBufferSize)
{
g_Notify->BreakPoint(__FILE__, __LINE__);
} }
void * MemAddr = CommitMemory(m_RecompCode + m_RecompSize, IncreaseCompileBufferSize, MEM_EXECUTE_READWRITE); void * MemAddr = CommitMemory(m_RecompCode + m_RecompSize, IncreaseCompileBufferSize, MEM_EXECUTE_READWRITE);
if (MemAddr == nullptr) if (MemAddr == nullptr)
@ -68,6 +76,7 @@ void CRecompMemory::CheckRecompMem()
g_Notify->FatalError(MSG_MEM_ALLOC_ERROR); g_Notify->FatalError(MSG_MEM_ALLOC_ERROR);
} }
m_RecompSize += IncreaseCompileBufferSize; m_RecompSize += IncreaseCompileBufferSize;
return true;
} }
void CRecompMemory::Reset() void CRecompMemory::Reset()

View File

@ -8,16 +8,17 @@ protected:
~CRecompMemory(); ~CRecompMemory();
bool AllocateMemory(); bool AllocateMemory();
void CheckRecompMem();
void Reset(); void Reset();
void ShowMemUsed(); void ShowMemUsed();
public: public:
uint8_t ** RecompPos() uint8_t *& RecompPos()
{ {
return &m_RecompPos; return m_RecompPos;
} }
bool CheckRecompMem(uint32_t BlockSize);
private: private:
CRecompMemory(const CRecompMemory &); CRecompMemory(const CRecompMemory &);
CRecompMemory & operator=(const CRecompMemory &); CRecompMemory & operator=(const CRecompMemory &);