#include "tconfig.h"
#include "tsystem.h"
#include "coretypes.h"
#include "tm.h"
#include "dwarf2.h"
#include "unwind.h"
#include "unwind-dw2.h"
#include <stdint.h>
#include <stdbool.h>
#include <signal.h>
#include <ucontext.h>
#include <mach/thread_status.h>
typedef unsigned long reg_unit;
static bool
interpret_libc (reg_unit gprs[32], struct _Unwind_Context *context)
{
uint32_t *pc = (uint32_t *)_Unwind_GetIP (context);
uint32_t cr;
reg_unit lr = (reg_unit) pc;
reg_unit ctr = 0;
uint32_t *invalid_address = NULL;
int i;
for (i = 0; i < 13; i++)
gprs[i] = 1;
gprs[1] = _Unwind_GetCFA (context);
for (; i < 32; i++)
gprs[i] = _Unwind_GetGR (context, i);
cr = _Unwind_GetGR (context, CR2_REGNO);
for (;;)
{
uint32_t ins = *pc++;
if ((ins & 0xFC000003) == 0x48000000)
{
pc += ((((int32_t) ins & 0x3FFFFFC) ^ 0x2000000) - 0x2000004) / 4;
continue;
}
if ((ins & 0xFC600000) == 0x2C000000)
{
int32_t val1 = (int16_t) ins;
int32_t val2 = gprs[ins >> 16 & 0x1F];
uint32_t mask = 0xF << ((ins >> 21 & 0x1C) ^ 0x1C);
if (val1 == val2)
cr |= mask;
else
cr &= ~mask;
continue;
}
if ((ins & 0xFEC38003) == 0x40820000)
{
if ((cr >> ((ins >> 16 & 0x1F) ^ 0x1F) & 1) == (ins >> 24 & 1))
pc += (ins & 0x7FFC) / 4 - 1;
continue;
}
if ((ins & 0xFC0007FF) == 0x7C000378)
{
gprs [ins >> 16 & 0x1F] = (gprs [ins >> 11 & 0x1F]
| gprs [ins >> 21 & 0x1F]);
continue;
}
if (ins >> 26 == 0x0E)
{
reg_unit src = (ins >> 16 & 0x1F) == 0 ? 0 : gprs [ins >> 16 & 0x1F];
gprs [ins >> 21 & 0x1F] = src + (int16_t) ins;
continue;
}
if (ins >> 26 == 0x0F)
{
reg_unit src = (ins >> 16 & 0x1F) == 0 ? 0 : gprs [ins >> 16 & 0x1F];
gprs [ins >> 21 & 0x1F] = src + ((int16_t) ins << 16);
continue;
}
if (ins >> 26 == 0x20)
{
reg_unit src = (ins >> 16 & 0x1F) == 0 ? 0 : gprs [ins >> 16 & 0x1F];
uint32_t *p = (uint32_t *)(src + (int16_t) ins);
if (p == invalid_address)
return false;
gprs [ins >> 21 & 0x1F] = *p;
continue;
}
if (ins >> 26 == 0x21)
{
uint32_t *p = (uint32_t *)(gprs [ins >> 16 & 0x1F] += (int16_t) ins);
if (p == invalid_address)
return false;
gprs [ins >> 21 & 0x1F] = *p;
continue;
}
if (ins >> 26 == 0x24)
{
reg_unit src = (ins >> 16 & 0x1F) == 0 ? 0 : gprs [ins >> 16 & 0x1F];
uint32_t *p = (uint32_t *)(src + (int16_t) ins);
if (p == NULL || invalid_address != NULL)
return false;
invalid_address = p;
continue;
}
if (ins >> 26 == 0x2E)
{
reg_unit src = (ins >> 16 & 0x1F) == 0 ? 0 : gprs [ins >> 16 & 0x1F];
uint32_t *p = (uint32_t *)(src + (int16_t) ins);
int i;
for (i = (ins >> 21 & 0x1F); i < 32; i++)
{
if (p == invalid_address)
return false;
gprs[i] = *p++;
}
continue;
}
if ((ins & 0xFC1FFFFF) == 0x7c0803a6)
{
lr = gprs [ins >> 21 & 0x1F];
continue;
}
if ((ins & 0xFC1FFFFF) == 0x7c0802a6)
{
gprs [ins >> 21 & 0x1F] = lr;
continue;
}
if ((ins & 0xFC1FFFFF) == 0x7c0903a6)
{
ctr = gprs [ins >> 21 & 0x1F];
continue;
}
if ((ins & 0xFC000FFF) == 0x7c000120)
{
int i;
uint32_t mask = 0;
for (i = 0; i < 8; i++)
mask |= ((-(ins >> (12 + i) & 1)) & 0xF) << 4 * i;
cr = (cr & ~mask) | (gprs [ins >> 21 & 0x1F] & mask);
continue;
}
if (ins == 0x429f0005)
{
lr = (reg_unit) pc;
continue;
}
if (ins == 0x4e800420)
{
pc = (uint32_t *) ctr;
continue;
}
if (ins == 0x44000002)
return true;
return false;
}
}
#define UC_TRAD 1
#define UC_TRAD_VEC 6
#define UC_TRAD64 20
#define UC_TRAD64_VEC 25
#define UC_FLAVOR 30
#define UC_FLAVOR_VEC 35
#define UC_FLAVOR64 40
#define UC_FLAVOR64_VEC 45
#define UC_DUAL 50
#define UC_DUAL_VEC 55
struct gcc_mcontext64 {
uint64_t dar;
uint32_t dsisr;
uint32_t exception;
uint32_t padding1[4];
uint64_t srr0;
uint64_t srr1;
uint32_t gpr[32][2];
uint32_t cr;
uint32_t xer[2];
uint32_t lr[2];
uint32_t ctr[2];
uint32_t vrsave;
ppc_float_state_t fs;
ppc_vector_state_t vs;
};
#define UC_FLAVOR_SIZE \
(sizeof (struct mcontext) - sizeof (ppc_vector_state_t))
#define UC_FLAVOR_VEC_SIZE (sizeof (struct mcontext))
#define UC_FLAVOR64_SIZE \
(sizeof (struct gcc_mcontext64) - sizeof (ppc_vector_state_t))
#define UC_FLAVOR64_VEC_SIZE (sizeof (struct gcc_mcontext64))
static bool
handle_syscall (_Unwind_FrameState *fs, const reg_unit gprs[32],
_Unwind_Ptr old_cfa)
{
ucontext_t *uctx;
bool is_64, is_vector;
ppc_float_state_t *float_state;
ppc_vector_state_t *vector_state;
_Unwind_Ptr new_cfa;
int i;
static _Unwind_Ptr return_addr;
if (gprs[0] == 0x67 )
{
uctx = (ucontext_t *) gprs[3];
is_vector = (uctx->uc_mcsize == UC_FLAVOR64_VEC_SIZE
|| uctx->uc_mcsize == UC_FLAVOR_VEC_SIZE);
is_64 = (uctx->uc_mcsize == UC_FLAVOR64_VEC_SIZE
|| uctx->uc_mcsize == UC_FLAVOR64_SIZE);
}
else if (gprs[0] == 0 && gprs[3] == 184)
{
int ctxstyle = gprs[5];
uctx = (ucontext_t *) gprs[4];
is_vector = (ctxstyle == UC_FLAVOR_VEC || ctxstyle == UC_FLAVOR64_VEC
|| ctxstyle == UC_TRAD_VEC || ctxstyle == UC_TRAD64_VEC);
is_64 = (ctxstyle == UC_FLAVOR64_VEC || ctxstyle == UC_TRAD64_VEC
|| ctxstyle == UC_FLAVOR64 || ctxstyle == UC_TRAD64);
}
else
return false;
#define set_offset(r, addr) \
(fs->regs.reg[r].how = REG_SAVED_OFFSET, \
fs->regs.reg[r].loc.offset = (_Unwind_Ptr)(addr) - new_cfa)
if (is_64)
{
struct gcc_mcontext64 *m64 = (struct gcc_mcontext64 *)uctx->uc_mcontext;
int i;
float_state = &m64->fs;
vector_state = &m64->vs;
new_cfa = m64->gpr[1][1];
set_offset (CR2_REGNO, &m64->cr);
for (i = 0; i < 32; i++)
set_offset (i, m64->gpr[i] + 1);
set_offset (XER_REGNO, m64->xer + 1);
set_offset (LINK_REGISTER_REGNUM, m64->lr + 1);
set_offset (COUNT_REGISTER_REGNUM, m64->ctr + 1);
if (is_vector)
set_offset (VRSAVE_REGNO, &m64->vrsave);
if (m64->exception == 3 || m64->exception == 4
|| m64->exception == 6
|| (m64->exception == 7 && !(m64->srr1 & 0x10000)))
return_addr = m64->srr0 + 4;
else
return_addr = m64->srr0;
}
else
{
struct mcontext *m = uctx->uc_mcontext;
int i;
float_state = &m->fs;
vector_state = &m->vs;
new_cfa = m->ss.r1;
set_offset (CR2_REGNO, &m->ss.cr);
for (i = 0; i < 32; i++)
set_offset (i, &m->ss.r0 + i);
set_offset (XER_REGNO, &m->ss.xer);
set_offset (LINK_REGISTER_REGNUM, &m->ss.lr);
set_offset (COUNT_REGISTER_REGNUM, &m->ss.ctr);
if (is_vector)
set_offset (VRSAVE_REGNO, &m->ss.vrsave);
if (m->es.exception == 3 || m->es.exception == 4
|| m->es.exception == 6
|| (m->es.exception == 7 && !(m->ss.srr1 & 0x10000)))
return_addr = m->ss.srr0 + 4;
else
return_addr = m->ss.srr0;
}
fs->cfa_how = CFA_REG_OFFSET;
fs->cfa_reg = STACK_POINTER_REGNUM;
fs->cfa_offset = new_cfa - old_cfa;;
fs->retaddr_column = ARG_POINTER_REGNUM;
set_offset (ARG_POINTER_REGNUM, &return_addr);
for (i = 0; i < 32; i++)
set_offset (32 + i, float_state->fpregs + i);
set_offset (SPEFSCR_REGNO, &float_state->fpscr);
if (is_vector)
{
for (i = 0; i < 32; i++)
set_offset (FIRST_ALTIVEC_REGNO + i, vector_state->save_vr + i);
set_offset (VSCR_REGNO, vector_state->save_vscr);
}
return true;
}
extern bool _Unwind_fallback_frame_state_for (struct _Unwind_Context *context,
_Unwind_FrameState *fs);
bool
_Unwind_fallback_frame_state_for (struct _Unwind_Context *context,
_Unwind_FrameState *fs)
{
reg_unit gprs[32];
if (!interpret_libc (gprs, context))
return false;
return handle_syscall (fs, gprs, _Unwind_GetCFA (context));
}