unicorn/qemu/target/arm/unicorn_aarch64.c

230 lines
8.6 KiB
C
Raw Normal View History

2015-08-21 07:04:50 +00:00
/* Unicorn Emulator Engine */
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2015 */
#include "qemu/osdep.h"
#include "cpu.h"
2015-08-21 07:04:50 +00:00
#include "hw/boards.h"
#include "hw/arm/arm.h"
#include "sysemu/cpus.h"
#include "unicorn.h"
#include "unicorn_common.h"
#include "uc_priv.h"
2015-08-21 07:04:50 +00:00
const int ARM64_REGS_STORAGE_SIZE = offsetof(CPUARMState, tlb_table);
2015-08-21 07:04:50 +00:00
static void arm64_set_pc(struct uc_struct *uc, uint64_t address)
{
CPUArchState *state = uc->cpu->env_ptr;
state->pc = address;
2015-08-21 07:04:50 +00:00
}
2016-01-31 22:22:20 +00:00
void arm64_release(void* ctx);
void arm64_release(void* ctx)
{
TCGContext *s = (TCGContext *) ctx;
struct uc_struct* uc = s->uc;
ARMCPU* cpu = ARM_CPU(uc, uc->cpu);
2016-01-31 22:22:20 +00:00
g_free(s->tb_ctx.tbs);
g_free(cpu->cpreg_indexes);
g_free(cpu->cpreg_values);
g_free(cpu->cpreg_vmstate_indexes);
g_free(cpu->cpreg_vmstate_values);
2016-01-31 22:22:20 +00:00
release_common(ctx);
}
2015-08-26 10:30:58 +00:00
void arm64_reg_reset(struct uc_struct *uc)
2015-08-21 07:04:50 +00:00
{
2016-09-23 14:38:21 +00:00
CPUArchState *env = uc->cpu->env_ptr;
2015-08-21 07:04:50 +00:00
memset(env->xregs, 0, sizeof(env->xregs));
env->pc = 0;
}
2016-04-04 15:25:30 +00:00
int arm64_reg_read(struct uc_struct *uc, unsigned int *regs, void **vals, int count)
2015-08-21 07:04:50 +00:00
{
2016-09-23 14:38:21 +00:00
CPUState *mycpu = uc->cpu;
CPUARMState *state = &ARM_CPU(uc, mycpu)->env;
2016-04-04 15:25:30 +00:00
int i;
2015-08-21 07:04:50 +00:00
2016-04-04 15:25:30 +00:00
for (i = 0; i < count; i++) {
unsigned int regid = regs[i];
void *value = vals[i];
// V & Q registers are the same
if (regid >= UC_ARM64_REG_V0 && regid <= UC_ARM64_REG_V31) {
regid += UC_ARM64_REG_Q0 - UC_ARM64_REG_V0;
}
if (regid >= UC_ARM64_REG_X0 && regid <= UC_ARM64_REG_X28) {
*(int64_t *)value = state->xregs[regid - UC_ARM64_REG_X0];
} else if (regid >= UC_ARM64_REG_W0 && regid <= UC_ARM64_REG_W30) {
*(int32_t *)value = READ_DWORD(state->xregs[regid - UC_ARM64_REG_W0]);
} else if (regid >= UC_ARM64_REG_Q0 && regid <= UC_ARM64_REG_Q31) {
float64 *dst = (float64*) value;
const float64 *q_reg = aa64_vfp_qreg(state, regid - UC_ARM64_REG_Q0);
dst[0] = q_reg[0];
dst[1] = q_reg[1];
} else if (regid >= UC_ARM64_REG_D0 && regid <= UC_ARM64_REG_D31) {
const float64 *q_reg = aa64_vfp_qreg(state, regid - UC_ARM64_REG_D0);
*(float64*)value = *q_reg;
} else if (regid >= UC_ARM64_REG_S0 && regid <= UC_ARM64_REG_S31) {
const float64 *q_reg = aa64_vfp_qreg(state, regid - UC_ARM64_REG_S0);
*(int32_t*)value = READ_DWORD(*q_reg);
} else if (regid >= UC_ARM64_REG_H0 && regid <= UC_ARM64_REG_H31) {
const float64 *q_reg = aa64_vfp_qreg(state, regid - UC_ARM64_REG_H0);
*(int16_t*)value = READ_WORD(*q_reg);
} else if (regid >= UC_ARM64_REG_B0 && regid <= UC_ARM64_REG_B31) {
const float64 *q_reg = aa64_vfp_qreg(state, regid - UC_ARM64_REG_B0);
*(int8_t*)value = READ_BYTE_L(*q_reg);
} else {
2016-04-04 15:25:30 +00:00
switch(regid) {
default: break;
case UC_ARM64_REG_CPACR_EL1:
*(uint32_t *)value = state->cp15.cpacr_el1;
break;
case UC_ARM64_REG_ESR:
*(uint32_t *)value = state->exception.syndrome;
break;
case UC_ARM64_REG_TPIDR_EL0:
*(int64_t *)value = state->cp15.tpidr_el[0];
break;
case UC_ARM64_REG_TPIDRRO_EL0:
*(int64_t *)value = state->cp15.tpidrro_el[0];
break;
case UC_ARM64_REG_TPIDR_EL1:
*(int64_t *)value = state->cp15.tpidr_el[1];
break;
2016-04-04 15:25:30 +00:00
case UC_ARM64_REG_X29:
*(int64_t *)value = state->xregs[29];
break;
2016-04-04 15:25:30 +00:00
case UC_ARM64_REG_X30:
*(int64_t *)value = state->xregs[30];
break;
2016-04-04 15:25:30 +00:00
case UC_ARM64_REG_PC:
*(uint64_t *)value = state->pc;
break;
2016-04-04 15:25:30 +00:00
case UC_ARM64_REG_SP:
*(int64_t *)value = state->xregs[31];
break;
case UC_ARM64_REG_NZCV:
*(int32_t *)value = cpsr_read(state) & CPSR_NZCV;
break;
2018-01-11 18:26:51 +00:00
case UC_ARM64_REG_PSTATE:
*(uint32_t *)value = pstate_read(state);
2018-01-11 18:26:51 +00:00
break;
2018-01-11 18:28:59 +00:00
case UC_ARM64_REG_FPCR:
*(uint32_t *)value = vfp_get_fpcr(state);
2018-01-11 18:28:59 +00:00
break;
case UC_ARM64_REG_FPSR:
*(uint32_t *)value = vfp_get_fpsr(state);
2018-01-11 18:28:59 +00:00
break;
2016-04-04 15:25:30 +00:00
}
2015-08-21 07:04:50 +00:00
}
}
return 0;
}
2016-04-04 15:25:30 +00:00
int arm64_reg_write(struct uc_struct *uc, unsigned int *regs, void* const* vals, int count)
2015-08-21 07:04:50 +00:00
{
2016-09-23 14:38:21 +00:00
CPUState *mycpu = uc->cpu;
CPUARMState *state = &ARM_CPU(uc, mycpu)->env;
2016-04-04 15:25:30 +00:00
int i;
2015-08-21 07:04:50 +00:00
2016-04-04 15:25:30 +00:00
for (i = 0; i < count; i++) {
unsigned int regid = regs[i];
const void *value = vals[i];
if (regid >= UC_ARM64_REG_V0 && regid <= UC_ARM64_REG_V31) {
regid += UC_ARM64_REG_Q0 - UC_ARM64_REG_V0;
}
if (regid >= UC_ARM64_REG_X0 && regid <= UC_ARM64_REG_X28) {
state->xregs[regid - UC_ARM64_REG_X0] = *(uint64_t *)value;
} else if (regid >= UC_ARM64_REG_W0 && regid <= UC_ARM64_REG_W30) {
WRITE_DWORD(state->xregs[regid - UC_ARM64_REG_W0], *(uint32_t *)value);
} else if (regid >= UC_ARM64_REG_Q0 && regid <= UC_ARM64_REG_Q31) {
const float64 *src = (const float64*) value;
float64 *q_reg = aa64_vfp_qreg(state, regid - UC_ARM64_REG_Q0);
q_reg[0] = src[0];
q_reg[1] = src[1];
} else if (regid >= UC_ARM64_REG_D0 && regid <= UC_ARM64_REG_D31) {
float64 *q_reg = aa64_vfp_qreg(state, regid - UC_ARM64_REG_D0);
*q_reg = *(float64*) value;
} else if (regid >= UC_ARM64_REG_S0 && regid <= UC_ARM64_REG_S31) {
float64 *q_reg = aa64_vfp_qreg(state, regid - UC_ARM64_REG_S0);
WRITE_DWORD(*q_reg, *(int32_t*) value);
} else if (regid >= UC_ARM64_REG_H0 && regid <= UC_ARM64_REG_H31) {
float64 *q_reg = aa64_vfp_qreg(state, regid - UC_ARM64_REG_H0);
WRITE_WORD(*q_reg, *(int16_t*) value);
} else if (regid >= UC_ARM64_REG_B0 && regid <= UC_ARM64_REG_B31) {
float64 *q_reg = aa64_vfp_qreg(state, regid - UC_ARM64_REG_B0);
WRITE_BYTE_L(*q_reg, *(int8_t*) value);
} else {
2016-04-04 15:25:30 +00:00
switch(regid) {
default: break;
case UC_ARM64_REG_CPACR_EL1:
state->cp15.cpacr_el1 = *(uint32_t *)value;
break;
case UC_ARM64_REG_TPIDR_EL0:
state->cp15.tpidr_el[0] = *(uint64_t *)value;
break;
case UC_ARM64_REG_TPIDRRO_EL0:
state->cp15.tpidrro_el[0] = *(uint64_t *)value;
break;
case UC_ARM64_REG_TPIDR_EL1:
state->cp15.tpidr_el[1] = *(uint64_t *)value;
break;
2016-04-04 15:25:30 +00:00
case UC_ARM64_REG_X29:
state->xregs[29] = *(uint64_t *)value;
break;
2016-04-04 15:25:30 +00:00
case UC_ARM64_REG_X30:
state->xregs[30] = *(uint64_t *)value;
break;
2016-04-04 15:25:30 +00:00
case UC_ARM64_REG_PC:
state->pc = *(uint64_t *)value;
// force to quit execution and flush TB
uc->quit_request = true;
uc_emu_stop(uc);
break;
2016-04-04 15:25:30 +00:00
case UC_ARM64_REG_SP:
state->xregs[31] = *(uint64_t *)value;
break;
case UC_ARM64_REG_NZCV:
cpsr_write(state, *(uint32_t *) value, CPSR_NZCV, CPSRWriteRaw);
break;
2018-01-11 18:26:51 +00:00
case UC_ARM64_REG_PSTATE:
pstate_write(state, *(uint32_t *)value);
2018-01-11 18:26:51 +00:00
break;
2018-01-11 18:28:59 +00:00
case UC_ARM64_REG_FPCR:
vfp_set_fpcr(state, *(uint32_t *)value);
2018-01-11 18:28:59 +00:00
break;
case UC_ARM64_REG_FPSR:
vfp_set_fpsr(state, *(uint32_t *)value);
2018-01-11 18:28:59 +00:00
break;
2016-04-04 15:25:30 +00:00
}
2015-08-21 07:04:50 +00:00
}
}
return 0;
}
DEFAULT_VISIBILITY
#ifdef TARGET_WORDS_BIGENDIAN
void arm64eb_uc_init(struct uc_struct* uc)
#else
2015-08-21 07:04:50 +00:00
void arm64_uc_init(struct uc_struct* uc)
#endif
2015-08-21 07:04:50 +00:00
{
register_accel_types(uc);
arm_cpu_register_types(uc);
aarch64_cpu_register_types(uc);
machvirt_machine_init(uc);
uc->reg_read = arm64_reg_read;
uc->reg_write = arm64_reg_write;
uc->reg_reset = arm64_reg_reset;
uc->set_pc = arm64_set_pc;
2016-01-31 22:22:20 +00:00
uc->release = arm64_release;
2015-08-21 07:04:50 +00:00
uc_common_init(uc);
}