2021-01-30 15:14:44 +00:00
|
|
|
#include "oslib/host_context.h"
|
2015-05-16 01:10:39 +00:00
|
|
|
|
2019-08-25 16:38:36 +00:00
|
|
|
#if defined(__ANDROID__)
|
2015-05-16 05:16:46 +00:00
|
|
|
#include <asm/sigcontext.h>
|
2015-05-16 01:10:39 +00:00
|
|
|
#else
|
2020-04-26 08:03:57 +00:00
|
|
|
#if defined(__APPLE__)
|
2015-05-16 05:16:46 +00:00
|
|
|
#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)
|
2021-01-30 15:14:44 +00:00
|
|
|
template <bool ToSegfault, typename Tctx, typename Tseg>
|
|
|
|
static void bicopy(Tctx& ctx, Tseg& seg)
|
|
|
|
{
|
|
|
|
static_assert(sizeof(Tctx) == sizeof(Tseg), "Invalid assignment");
|
|
|
|
if (ToSegfault)
|
|
|
|
seg = (Tseg)ctx;
|
|
|
|
else
|
|
|
|
ctx = (Tctx)seg;
|
2015-05-16 01:10:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 15:14:44 +00:00
|
|
|
template<bool ToSegfault>
|
|
|
|
static void context_segfault(host_context_t* hostctx, void* segfault_ctx)
|
|
|
|
{
|
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__)
|
2021-01-30 15:14:44 +00:00
|
|
|
bicopy<ToSegfault>(hostctx->pc, MCTX(.__gregs[_REG_PC]));
|
2018-03-06 22:54:46 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 15; i++)
|
2021-01-30 15:14:44 +00:00
|
|
|
bicopy<ToSegfault>(hostctx->reg[i], MCTX(.__gregs[i]));
|
2018-03-06 22:54:46 +00:00
|
|
|
#elif HOST_OS == OS_LINUX
|
2021-01-30 15:14:44 +00:00
|
|
|
bicopy<ToSegfault>(hostctx->pc, MCTX(.arm_pc));
|
|
|
|
u32* reg =(u32*) &MCTX(.arm_r0);
|
2015-05-16 05:16:46 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 15; i++)
|
2021-01-30 15:14:44 +00:00
|
|
|
bicopy<ToSegfault>(hostctx->reg[i], reg[i]);
|
2015-05-16 05:16:46 +00:00
|
|
|
|
2020-04-26 08:03:57 +00:00
|
|
|
#elif defined(__APPLE__)
|
2021-01-30 15:14:44 +00:00
|
|
|
bicopy<ToSegfault>(hostctx->pc, MCTX(->__ss.__pc));
|
2015-05-16 05:16:46 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 15; i++)
|
2021-01-30 15:14:44 +00:00
|
|
|
bicopy<ToSegfault>(hostctx->reg[i], MCTX(->__ss.__r[i]));
|
2015-05-16 05:16:46 +00:00
|
|
|
#else
|
|
|
|
#error HOST_OS
|
|
|
|
#endif
|
2019-01-07 20:50:46 +00:00
|
|
|
#elif HOST_CPU == CPU_ARM64
|
2021-03-03 01:18:19 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
bicopy<ToSegfault>(hostctx->pc, MCTX(->__ss.__pc));
|
|
|
|
bicopy<ToSegfault>(hostctx->sp, MCTX(->__ss.__sp));
|
|
|
|
bicopy<ToSegfault>(hostctx->x2, MCTX(->__ss.__x[2]));
|
|
|
|
#else
|
|
|
|
bicopy<ToSegfault>(hostctx->pc, MCTX(.pc));
|
|
|
|
bicopy<ToSegfault>(hostctx->sp, MCTX(.sp));
|
|
|
|
bicopy<ToSegfault>(hostctx->x2, MCTX(.regs[2]));
|
|
|
|
#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__)
|
2021-01-30 15:14:44 +00:00
|
|
|
bicopy<ToSegfault>(hostctx->pc, MCTX(.mc_eip));
|
|
|
|
bicopy<ToSegfault>(hostctx->esp, MCTX(.mc_esp));
|
|
|
|
bicopy<ToSegfault>(hostctx->eax, MCTX(.mc_eax));
|
|
|
|
bicopy<ToSegfault>(hostctx->ecx, MCTX(.mc_ecx));
|
2018-03-06 22:54:46 +00:00
|
|
|
#elif HOST_OS == OS_LINUX
|
2021-01-30 15:14:44 +00:00
|
|
|
bicopy<ToSegfault>(hostctx->pc, MCTX(.gregs[REG_EIP]));
|
|
|
|
bicopy<ToSegfault>(hostctx->esp, MCTX(.gregs[REG_ESP]));
|
|
|
|
bicopy<ToSegfault>(hostctx->eax, MCTX(.gregs[REG_EAX]));
|
|
|
|
bicopy<ToSegfault>(hostctx->ecx, MCTX(.gregs[REG_ECX]));
|
2020-04-26 08:03:57 +00:00
|
|
|
#elif defined(__APPLE__)
|
2021-01-30 15:14:44 +00:00
|
|
|
bicopy<ToSegfault>(hostctx->pc, MCTX(->__ss.__eip));
|
|
|
|
bicopy<ToSegfault>(hostctx->esp, MCTX(->__ss.__esp));
|
|
|
|
bicopy<ToSegfault>(hostctx->eax, MCTX(->__ss.__eax));
|
|
|
|
bicopy<ToSegfault>(hostctx->ecx, MCTX(->__ss.__ecx));
|
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__)
|
2021-01-30 15:14:44 +00:00
|
|
|
bicopy<ToSegfault>(hostctx->pc, MCTX(.mc_rip));
|
2018-03-06 22:54:46 +00:00
|
|
|
#elif defined(__NetBSD__)
|
2021-01-30 15:14:44 +00:00
|
|
|
bicopy<ToSegfault>(hostctx->pc, MCTX(.__gregs[_REG_RIP]));
|
|
|
|
bicopy<ToSegfault>(hostctx->rsp, MCTX(.__gregs[REG_RSP]));
|
|
|
|
bicopy<ToSegfault>(hostctx->r9, MCTX(.__gregs[REG_R9]));
|
|
|
|
bicopy<ToSegfault>(hostctx->rdi, MCTX(.__gregs[REG_RDI]));
|
2018-03-06 22:54:46 +00:00
|
|
|
#elif HOST_OS == OS_LINUX
|
2021-01-30 15:14:44 +00:00
|
|
|
bicopy<ToSegfault>(hostctx->pc, MCTX(.gregs[REG_RIP]));
|
|
|
|
bicopy<ToSegfault>(hostctx->rsp, MCTX(.gregs[REG_RSP]));
|
|
|
|
bicopy<ToSegfault>(hostctx->r9, MCTX(.gregs[REG_R9]));
|
|
|
|
bicopy<ToSegfault>(hostctx->rdi, MCTX(.gregs[REG_RDI]));
|
2020-04-26 08:03:57 +00:00
|
|
|
#elif defined(__APPLE__)
|
2021-01-30 15:14:44 +00:00
|
|
|
bicopy<ToSegfault>(hostctx->pc, MCTX(->__ss.__rip));
|
|
|
|
bicopy<ToSegfault>(hostctx->rsp, MCTX(->__ss.__rsp));
|
|
|
|
bicopy<ToSegfault>(hostctx->r9, MCTX(->__ss.__r9));
|
|
|
|
bicopy<ToSegfault>(hostctx->rdi, MCTX(->__ss.__rdi));
|
2018-08-01 16:31:20 +00:00
|
|
|
#else
|
|
|
|
#error HOST_OS
|
2018-03-06 22:54:46 +00:00
|
|
|
#endif
|
2015-05-16 01:10:39 +00:00
|
|
|
#elif HOST_CPU == CPU_MIPS
|
2021-01-30 15:14:44 +00:00
|
|
|
bicopy<ToSegfault>(hostctx->pc, MCTX(.pc));
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-30 15:14:44 +00:00
|
|
|
void context_from_segfault(host_context_t* hostctx, void* segfault_ctx) {
|
|
|
|
context_segfault<false>(hostctx, segfault_ctx);
|
2015-05-16 01:10:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 15:14:44 +00:00
|
|
|
void context_to_segfault(host_context_t* hostctx, void* segfault_ctx) {
|
|
|
|
context_segfault<true>(hostctx, segfault_ctx);
|
2015-07-14 01:35:34 +00:00
|
|
|
}
|