Define CONTEXT_PC and use instead for much cleaner code (#1017)

* Define CONTEXT_PC and use instead for much cleaner code

Also include ways to get the Program Counter on ARM64 FreeBSD and NetBSD
This commit is contained in:
WaluigiWare64 2021-02-25 21:17:11 +00:00 committed by GitHub
parent 81980d2836
commit 89051f63d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 additions and 39 deletions

View File

@ -63,6 +63,33 @@ struct FaultDescription
bool FaultHandler(FaultDescription& faultDesc);
}
// Yes I know this looks messy, but better here than somewhere else in the code
#if defined(_WIN32)
#define CONTEXT_PC Rip
#else
#if defined(__x86_64__)
#if defined(__linux__)
#define CONTEXT_PC uc_mcontext.gregs[REG_RIP]
#elif defined(__APPLE__)
#define CONTEXT_PC uc_mcontext->__ss.__rip
#elif defined(__FreeBSD__)
#define CONTEXT_PC uc_mcontext.mc_rip
#elif defined(__NetBSD__)
#define CONTEXT_PC uc_mcontext.__gregs[_REG_RIP]
#endif
#elif defined(__aarch64__)
#if defined(__linux__)
#define CONTEXT_PC uc_mcontext.pc
#elif defined(__APPLE__)
#define CONTEXT_PC uc_mcontext->__ss.__pc
#elif defined(__FreeBSD__)
#define CONTEXT_PC uc_mcontext.mc_gpregs.gp_elr
#elif defined(__NetBSD__)
#define CONTEXT_PC uc_mcontext.__gregs[_REG_PC]
#endif
#endif
#endif
#if defined(__ANDROID__)
#define ASHMEM_DEVICE "/dev/ashmem"
#endif
@ -120,11 +147,11 @@ static LONG ExceptionHandler(EXCEPTION_POINTERS* exceptionInfo)
ARMJIT_Memory::FaultDescription desc;
u8* curArea = (u8*)(NDS::CurCPU == 0 ? ARMJIT_Memory::FastMem9Start : ARMJIT_Memory::FastMem7Start);
desc.EmulatedFaultAddr = (u8*)exceptionInfo->ExceptionRecord->ExceptionInformation[1] - curArea;
desc.FaultPC = (u8*)exceptionInfo->ContextRecord->Rip;
desc.FaultPC = (u8*)exceptionInfo->ContextRecord->CONTEXT_PC;
if (ARMJIT_Memory::FaultHandler(desc))
{
exceptionInfo->ContextRecord->Rip = (u64)desc.FaultPC;
exceptionInfo->ContextRecord->CONTEXT_PC = (u64)desc.FaultPC;
return EXCEPTION_CONTINUE_EXECUTION;
}
@ -153,47 +180,13 @@ static void SigsegvHandler(int sig, siginfo_t* info, void* rawContext)
ARMJIT_Memory::FaultDescription desc;
u8* curArea = (u8*)(NDS::CurCPU == 0 ? ARMJIT_Memory::FastMem9Start : ARMJIT_Memory::FastMem7Start);
#ifdef __x86_64__
desc.EmulatedFaultAddr = (u8*)info->si_addr - curArea;
#if defined(__APPLE__)
desc.FaultPC = (u8*)context->uc_mcontext->__ss.__rip;
#elif defined(__FreeBSD__)
desc.FaultPC = (u8*)context->uc_mcontext.mc_rip;
#elif defined(__NetBSD__)
desc.FaultPC = (u8*)context->uc_mcontext.__gregs[_REG_RIP];
#else
desc.FaultPC = (u8*)context->uc_mcontext.gregs[REG_RIP];
#endif
#else
#ifdef __APPLE__
desc.EmulatedFaultAddr = (u8*)context->uc_mcontext->__es.__far - curArea;
desc.FaultPC = (u8*)context->uc_mcontext->__ss.__pc;
#else
desc.EmulatedFaultAddr = (u8*)context->uc_mcontext.fault_address - curArea;
desc.FaultPC = (u8*)context->uc_mcontext.pc;
#endif
#endif
desc.FaultPC = (u8*)context->CONTEXT_PC;
if (ARMJIT_Memory::FaultHandler(desc))
{
#ifdef __x86_64__
#if defined(__APPLE__)
context->uc_mcontext->__ss.__rip = (u64)desc.FaultPC;
#elif defined(__FreeBSD__)
context->uc_mcontext.mc_rip = (u64)desc.FaultPC;
#elif defined(__NetBSD__)
context->uc_mcontext.__gregs[_REG_RIP] = (u64)desc.FaultPC;
#else
context->uc_mcontext.gregs[REG_RIP] = (u64)desc.FaultPC;
#endif
#else
#ifdef __APPLE__
context->uc_mcontext->__ss.__pc = (u64)desc.FaultPC;
#else
context->uc_mcontext.pc = (u64)desc.FaultPC;
#endif
#endif
context->CONTEXT_PC = (u64)desc.FaultPC;
return;
}