2015-05-16 01:10:39 +00:00
|
|
|
#include "context.h"
|
|
|
|
|
|
|
|
#if defined(_ANDROID)
|
2015-05-16 05:16:46 +00:00
|
|
|
#include <asm/sigcontext.h>
|
2015-05-16 01:10:39 +00:00
|
|
|
#else
|
2015-05-16 05:16:46 +00:00
|
|
|
#if HOST_OS == OS_DARWIN
|
|
|
|
#define _XOPEN_SOURCE 1
|
|
|
|
#define __USE_GNU 1
|
|
|
|
#endif
|
|
|
|
|
2015-07-28 11:09:27 +00:00
|
|
|
#if !defined(TARGET_NO_EXCEPTIONS)
|
|
|
|
#include <ucontext.h>
|
|
|
|
#endif
|
2015-05-16 01:10:39 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
//////
|
|
|
|
|
2015-05-16 05:16:46 +00:00
|
|
|
#define MCTX(p) (((ucontext_t *)(segfault_ctx))->uc_mcontext p)
|
2015-05-16 01:10:39 +00:00
|
|
|
template <typename Ta, typename Tb>
|
2015-05-16 06:18:46 +00:00
|
|
|
void bicopy(Ta& rei, Tb& seg, bool to_segfault) {
|
|
|
|
if (to_segfault) {
|
|
|
|
seg = rei;
|
2015-05-16 01:10:39 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-05-16 06:18:46 +00:00
|
|
|
rei = seg;
|
2015-05-16 01:10:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void context_segfault(rei_host_context_t* reictx, void* segfault_ctx, bool to_segfault) {
|
|
|
|
|
2015-07-28 11:09:27 +00:00
|
|
|
#if !defined(TARGET_NO_EXCEPTIONS)
|
2015-05-16 01:10:39 +00:00
|
|
|
#if HOST_CPU == CPU_ARM
|
2018-03-06 22:54:46 +00:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
bicopy(reictx->pc, MCTX(.__gregs[_REG_PC]), to_segfault);
|
|
|
|
|
|
|
|
for (int i = 0; i < 15; i++)
|
|
|
|
bicopy(reictx->r[i], MCTX(.__gregs[i]), to_segfault);
|
|
|
|
#elif HOST_OS == OS_LINUX
|
2015-05-16 06:18:46 +00:00
|
|
|
bicopy(reictx->pc, MCTX(.arm_pc), to_segfault);
|
2015-05-16 05:16:46 +00:00
|
|
|
u32* r =(u32*) &MCTX(.arm_r0);
|
|
|
|
|
|
|
|
for (int i = 0; i < 15; i++)
|
2015-05-16 06:18:46 +00:00
|
|
|
bicopy(reictx->r[i], r[i], to_segfault);
|
2015-05-16 05:16:46 +00:00
|
|
|
|
|
|
|
#elif HOST_OS == OS_DARWIN
|
2015-05-16 06:18:46 +00:00
|
|
|
bicopy(reictx->pc, MCTX(->__ss.__pc), to_segfault);
|
2015-05-16 05:16:46 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 15; i++)
|
2015-05-16 06:18:46 +00:00
|
|
|
bicopy(reictx->r[i], MCTX(->__ss.__r[i]), to_segfault);
|
2015-05-16 05:16:46 +00:00
|
|
|
#else
|
|
|
|
#error HOST_OS
|
|
|
|
#endif
|
2015-05-16 01:10:39 +00:00
|
|
|
#elif HOST_CPU == CPU_X86
|
2018-03-06 22:54:46 +00:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
bicopy(reictx->pc, MCTX(.mc_eip), to_segfault);
|
|
|
|
bicopy(reictx->esp, MCTX(.mc_esp), to_segfault);
|
|
|
|
bicopy(reictx->eax, MCTX(.mc_eax), to_segfault);
|
|
|
|
bicopy(reictx->ecx, MCTX(.mc_ecx), to_segfault);
|
|
|
|
#elif HOST_OS == OS_LINUX
|
2015-05-16 06:18:46 +00:00
|
|
|
bicopy(reictx->pc, MCTX(.gregs[REG_EIP]), to_segfault);
|
|
|
|
bicopy(reictx->esp, MCTX(.gregs[REG_ESP]), to_segfault);
|
|
|
|
bicopy(reictx->eax, MCTX(.gregs[REG_EAX]), to_segfault);
|
|
|
|
bicopy(reictx->ecx, MCTX(.gregs[REG_ECX]), to_segfault);
|
2015-05-16 05:16:46 +00:00
|
|
|
#elif HOST_OS == OS_DARWIN
|
2015-05-16 06:18:46 +00:00
|
|
|
bicopy(reictx->pc, MCTX(->__ss.__eip), to_segfault);
|
|
|
|
bicopy(reictx->esp, MCTX(->__ss.__esp), to_segfault);
|
|
|
|
bicopy(reictx->eax, MCTX(->__ss.__eax), to_segfault);
|
|
|
|
bicopy(reictx->ecx, MCTX(->__ss.__ecx), to_segfault);
|
2015-05-16 05:16:46 +00:00
|
|
|
#else
|
|
|
|
#error HOST_OS
|
|
|
|
#endif
|
2015-07-14 01:35:34 +00:00
|
|
|
#elif HOST_CPU == CPU_X64
|
2018-03-06 22:54:46 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
|
|
|
bicopy(reictx->pc, MCTX(.mc_rip), to_segfault);
|
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
bicopy(reictx->pc, MCTX(.__gregs[_REG_RIP]), to_segfault);
|
|
|
|
#elif HOST_OS == OS_LINUX
|
|
|
|
bicopy(reictx->pc, MCTX(.gregs[REG_RIP]), to_segfault);
|
|
|
|
#else
|
|
|
|
#error HOST_OS
|
|
|
|
#endif
|
2015-05-16 01:10:39 +00:00
|
|
|
#elif HOST_CPU == CPU_MIPS
|
2015-05-16 06:18:46 +00:00
|
|
|
bicopy(reictx->pc, MCTX(.pc), to_segfault);
|
2015-08-05 19:04:17 +00:00
|
|
|
#elif HOST_CPU == CPU_GENERIC
|
|
|
|
//nothing!
|
2015-05-16 01:10:39 +00:00
|
|
|
#else
|
2015-05-16 05:16:46 +00:00
|
|
|
#error Unsupported HOST_CPU
|
2015-05-16 01:10:39 +00:00
|
|
|
#endif
|
2015-07-28 11:09:27 +00:00
|
|
|
#endif
|
2015-05-16 01:10:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void context_from_segfault(rei_host_context_t* reictx, void* segfault_ctx) {
|
|
|
|
context_segfault(reictx, segfault_ctx, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void context_to_segfault(rei_host_context_t* reictx, void* segfault_ctx) {
|
|
|
|
context_segfault(reictx, segfault_ctx, true);
|
2015-07-14 01:35:34 +00:00
|
|
|
}
|