Skip to content

Commit

Permalink
Merge pull request #1025 from comex/blr-optimization
Browse files Browse the repository at this point in the history
Opportunistically predict BLR destinations using RET.
  • Loading branch information
comex committed Sep 18, 2014
2 parents 5fafcb6 + 7ad9027 commit 49a48a6
Show file tree
Hide file tree
Showing 30 changed files with 598 additions and 292 deletions.
19 changes: 19 additions & 0 deletions Source/Core/Common/MemoryUtil.cpp
Expand Up @@ -158,6 +158,25 @@ void FreeAlignedMemory(void* ptr)
}
}

void ReadProtectMemory(void* ptr, size_t size)
{
bool error_occurred = false;

#ifdef _WIN32
DWORD oldValue;
if (!VirtualProtect(ptr, size, PAGE_NOACCESS, &oldValue))
error_occurred = true;
#else
int retval = mprotect(ptr, size, PROT_NONE);

if (retval != 0)
error_occurred = true;
#endif

if (error_occurred)
PanicAlert("ReadProtectMemory failed!\n%s", GetLastErrorMsg());
}

void WriteProtectMemory(void* ptr, size_t size, bool allowExecute)
{
bool error_occurred = false;
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/Common/MemoryUtil.h
Expand Up @@ -12,8 +12,12 @@ void* AllocateMemoryPages(size_t size);
void FreeMemoryPages(void* ptr, size_t size);
void* AllocateAlignedMemory(size_t size,size_t alignment);
void FreeAlignedMemory(void* ptr);
void ReadProtectMemory(void* ptr, size_t size);
void WriteProtectMemory(void* ptr, size_t size, bool executable = false);
void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute = false);
std::string MemUsage();

void GuardMemoryMake(void* ptr, size_t size);
void GuardMemoryUnmake(void* ptr, size_t size);

inline int GetPageSize() { return 4096; }
2 changes: 2 additions & 0 deletions Source/Core/Common/x64Emitter.cpp
Expand Up @@ -1766,6 +1766,8 @@ void XEmitter::ANDN(int bits, X64Reg regOp1, X64Reg regOp2, OpArg arg) {WriteBMI
void XEmitter::LOCK() { Write8(0xF0); }
void XEmitter::REP() { Write8(0xF3); }
void XEmitter::REPNE() { Write8(0xF2); }
void XEmitter::FSOverride() { Write8(0x64); }
void XEmitter::GSOverride() { Write8(0x65); }

void XEmitter::FWAIT()
{
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/Common/x64Emitter.h
Expand Up @@ -467,6 +467,8 @@ class XEmitter
void LOCK();
void REP();
void REPNE();
void FSOverride();
void GSOverride();

// x87
enum x87StatusWordBits {
Expand Down
36 changes: 12 additions & 24 deletions Source/Core/Core/ArmMemTools.cpp
Expand Up @@ -32,9 +32,9 @@ typedef struct ucontext {
} ucontext_t;
#endif

void sigsegv_handler(int signal, siginfo_t *info, void *raw_context)
static void sigsegv_handler(int sig, siginfo_t *info, void *raw_context)
{
if (signal != SIGSEGV)
if (sig != SIGSEGV)
{
// We are not interested in other signals - handle it as usual.
return;
Expand All @@ -47,33 +47,18 @@ void sigsegv_handler(int signal, siginfo_t *info, void *raw_context)
return;
}


// Get all the information we can out of the context.
mcontext_t *ctx = &context->uc_mcontext;

void *fault_memory_ptr = (void*)ctx->arm_r10;
u8 *fault_instruction_ptr = (u8 *)ctx->arm_pc;

if (!JitInterface::IsInCodeSpace(fault_instruction_ptr))
{
// Let's not prevent debugging.
return;
}
// comex says hello, and is most curious whether this is arm_r10 for a
// reason as opposed to si_addr like the x64MemTools.cpp version. Is there
// even a need for this file to be architecture specific?
uintptr_t fault_memory_ptr = (uintptr_t)ctx->arm_r10;

u64 bad_address = (u64)fault_memory_ptr;
u64 memspace_bottom = (u64)Memory::base;
if (bad_address < memspace_bottom)
if (!JitInterface::HandleFault(fault_memory_ptr, ctx))
{
PanicAlertT("Exception handler - access below memory space. %08llx%08llx",
bad_address >> 32, bad_address);
}

u32 em_address = (u32)(bad_address - memspace_bottom);

const u8 *new_rip = jit->BackPatch(fault_instruction_ptr, em_address, ctx);
if (new_rip)
{
ctx->arm_pc = (u32) new_rip;
// retry and crash
signal(SIGSEGV, SIG_DFL);
}
}

Expand All @@ -86,4 +71,7 @@ void InstallExceptionHandler()
sigemptyset(&sa.sa_mask);
sigaction(SIGSEGV, &sa, nullptr);
}

void UninstallExceptionHandler() {}

} // namespace
5 changes: 3 additions & 2 deletions Source/Core/Core/CMakeLists.txt
Expand Up @@ -195,9 +195,10 @@ if(_M_X86)
PowerPC/Jit64/Jit_Paired.cpp
PowerPC/Jit64/JitRegCache.cpp
PowerPC/Jit64/Jit_SystemRegisters.cpp
PowerPC/JitCommon/JitBackpatch.cpp
PowerPC/JitCommon/JitAsmCommon.cpp
PowerPC/JitCommon/Jit_Util.cpp)
PowerPC/JitCommon/JitBackpatch.cpp
PowerPC/JitCommon/Jit_Util.cpp
PowerPC/JitCommon/TrampolineCache.cpp)
elseif(_M_ARM_32)
set(SRCS ${SRCS}
ArmMemTools.cpp
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/Core/Core.cpp
Expand Up @@ -277,6 +277,10 @@ static void CpuThread()
if (!_CoreParameter.bCPUThread)
g_video_backend->Video_Cleanup();

#if _M_X86_64 || _M_ARM_32
EMM::UninstallExceptionHandler();
#endif

return;
}

Expand Down
4 changes: 3 additions & 1 deletion Source/Core/Core/Core.vcxproj
Expand Up @@ -229,6 +229,7 @@
<ClCompile Include="PowerPC\JitCommon\JitBase.cpp" />
<ClCompile Include="PowerPC\JitCommon\JitCache.cpp" />
<ClCompile Include="PowerPC\JitCommon\Jit_Util.cpp" />
<ClCompile Include="PowerPC\JitCommon\TrampolineCache.cpp" />
<ClCompile Include="PowerPC\JitInterface.cpp" />
<ClCompile Include="PowerPC\PowerPC.cpp" />
<ClCompile Include="PowerPC\PPCAnalyst.cpp" />
Expand Down Expand Up @@ -406,6 +407,7 @@
<ClInclude Include="PowerPC\JitCommon\JitBase.h" />
<ClInclude Include="PowerPC\JitCommon\JitCache.h" />
<ClInclude Include="PowerPC\JitCommon\Jit_Util.h" />
<ClInclude Include="PowerPC\JitCommon\TrampolineCache.h" />
<ClInclude Include="PowerPC\JitInterface.h" />
<ClInclude Include="PowerPC\PowerPC.h" />
<ClInclude Include="PowerPC\PPCAnalyst.h" />
Expand Down Expand Up @@ -464,4 +466,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
8 changes: 7 additions & 1 deletion Source/Core/Core/Core.vcxproj.filters
Expand Up @@ -640,6 +640,9 @@
<ClCompile Include="PowerPC\JitCommon\JitCache.cpp">
<Filter>PowerPC\JitCommon</Filter>
</ClCompile>
<ClCompile Include="PowerPC\JitCommon\TrampolineCache.cpp">
<Filter>PowerPC\JitCommon</Filter>
</ClCompile>
<ClCompile Include="PowerPC\Jit64IL\IR_X86.cpp">
<Filter>PowerPC\JitIL</Filter>
</ClCompile>
Expand Down Expand Up @@ -1182,6 +1185,9 @@
<ClInclude Include="PowerPC\JitCommon\JitCache.h">
<Filter>PowerPC\JitCommon</Filter>
</ClInclude>
<ClInclude Include="PowerPC\JitCommon\TrampolineCache.h">
<Filter>PowerPC\JitCommon</Filter>
</ClInclude>
<ClInclude Include="PowerPC\Jit64IL\JitIL.h">
<Filter>PowerPC\JitIL</Filter>
</ClInclude>
Expand All @@ -1204,4 +1210,4 @@
<ItemGroup>
<Text Include="CMakeLists.txt" />
</ItemGroup>
</Project>
</Project>
1 change: 1 addition & 0 deletions Source/Core/Core/MemTools.h
Expand Up @@ -11,4 +11,5 @@ namespace EMM
{
typedef u32 EAddr;
void InstallExceptionHandler();
void UninstallExceptionHandler();
}

0 comments on commit 49a48a6

Please sign in to comment.