2015-08-21 10:04:50 +03:00
|
|
|
/* Unicorn Emulator Engine */
|
|
|
|
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2015 */
|
|
|
|
|
|
|
|
#include "hw/boards.h"
|
|
|
|
#include "hw/arm/arm.h"
|
|
|
|
#include "sysemu/cpus.h"
|
|
|
|
#include "unicorn.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "unicorn_common.h"
|
2016-03-02 06:43:02 +03:00
|
|
|
#include "uc_priv.h"
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
|
2016-08-20 14:14:07 +03:00
|
|
|
const int ARM64_REGS_STORAGE_SIZE = offsetof(CPUARMState, tlb_table);
|
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
static void arm64_set_pc(struct uc_struct *uc, uint64_t address)
|
|
|
|
{
|
|
|
|
((CPUARMState *)uc->current_cpu->env_ptr)->pc = address;
|
|
|
|
}
|
|
|
|
|
2016-02-01 01:22:20 +03:00
|
|
|
void arm64_release(void* ctx);
|
|
|
|
|
|
|
|
void arm64_release(void* ctx)
|
|
|
|
{
|
2017-01-23 18:30:57 +03:00
|
|
|
struct uc_struct* uc;
|
|
|
|
ARMCPU* cpu;
|
2016-02-01 01:22:20 +03:00
|
|
|
TCGContext *s = (TCGContext *) ctx;
|
|
|
|
|
2016-12-21 17:28:36 +03:00
|
|
|
g_free(s->tb_ctx.tbs);
|
2017-01-23 18:30:57 +03:00
|
|
|
uc = s->uc;
|
|
|
|
cpu = (ARMCPU*) uc->cpu;
|
2016-12-21 17:28:36 +03:00
|
|
|
g_free(cpu->cpreg_indexes);
|
|
|
|
g_free(cpu->cpreg_values);
|
|
|
|
g_free(cpu->cpreg_vmstate_indexes);
|
|
|
|
g_free(cpu->cpreg_vmstate_values);
|
2016-02-01 01:22:20 +03:00
|
|
|
|
|
|
|
release_common(ctx);
|
|
|
|
}
|
|
|
|
|
2015-08-26 13:30:58 +03:00
|
|
|
void arm64_reg_reset(struct uc_struct *uc)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
2016-09-23 17:38:21 +03:00
|
|
|
CPUArchState *env = uc->cpu->env_ptr;
|
2015-08-21 10:04:50 +03:00
|
|
|
memset(env->xregs, 0, sizeof(env->xregs));
|
|
|
|
|
|
|
|
env->pc = 0;
|
|
|
|
}
|
|
|
|
|
2016-04-04 18:25:30 +03:00
|
|
|
int arm64_reg_read(struct uc_struct *uc, unsigned int *regs, void **vals, int count)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
2016-09-23 17:38:21 +03:00
|
|
|
CPUState *mycpu = uc->cpu;
|
2016-04-04 18:25:30 +03:00
|
|
|
int i;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2016-04-04 18:25:30 +03:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
unsigned int regid = regs[i];
|
|
|
|
void *value = vals[i];
|
2017-03-07 16:29:34 +03:00
|
|
|
// 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;
|
|
|
|
}
|
2017-01-15 15:13:35 +03:00
|
|
|
if (regid >= UC_ARM64_REG_X0 && regid <= UC_ARM64_REG_X28) {
|
2016-04-04 18:25:30 +03:00
|
|
|
*(int64_t *)value = ARM_CPU(uc, mycpu)->env.xregs[regid - UC_ARM64_REG_X0];
|
2017-01-15 15:13:35 +03:00
|
|
|
} else if (regid >= UC_ARM64_REG_W0 && regid <= UC_ARM64_REG_W30) {
|
|
|
|
*(int32_t *)value = READ_DWORD(ARM_CPU(uc, mycpu)->env.xregs[regid - UC_ARM64_REG_W0]);
|
2017-03-07 16:29:34 +03:00
|
|
|
} else if (regid >= UC_ARM64_REG_Q0 && regid <= UC_ARM64_REG_Q31) {
|
|
|
|
float64 *dst = (float64*) value;
|
|
|
|
uint32_t reg_index = 2*(regid - UC_ARM64_REG_Q0);
|
|
|
|
dst[0] = ARM_CPU(uc, mycpu)->env.vfp.regs[reg_index];
|
|
|
|
dst[1] = ARM_CPU(uc, mycpu)->env.vfp.regs[reg_index+1];
|
|
|
|
} else if (regid >= UC_ARM64_REG_D0 && regid <= UC_ARM64_REG_D31) {
|
|
|
|
*(float64*)value = ARM_CPU(uc, mycpu)->env.vfp.regs[2*(regid - UC_ARM64_REG_D0)];
|
|
|
|
} else if (regid >= UC_ARM64_REG_S0 && regid <= UC_ARM64_REG_S31) {
|
|
|
|
*(int32_t*)value = READ_DWORD(ARM_CPU(uc, mycpu)->env.vfp.regs[2*(regid - UC_ARM64_REG_S0)]);
|
|
|
|
} else if (regid >= UC_ARM64_REG_H0 && regid <= UC_ARM64_REG_H31) {
|
|
|
|
*(int16_t*)value = READ_WORD(ARM_CPU(uc, mycpu)->env.vfp.regs[2*(regid - UC_ARM64_REG_H0)]);
|
|
|
|
} else if (regid >= UC_ARM64_REG_B0 && regid <= UC_ARM64_REG_B31) {
|
|
|
|
*(int8_t*)value = READ_BYTE_L(ARM_CPU(uc, mycpu)->env.vfp.regs[2*(regid - UC_ARM64_REG_B0)]);
|
2017-01-15 15:13:35 +03:00
|
|
|
} else {
|
2016-04-04 18:25:30 +03:00
|
|
|
switch(regid) {
|
|
|
|
default: break;
|
2017-05-02 09:51:19 +03:00
|
|
|
case UC_ARM64_REG_CPACR_EL1:
|
|
|
|
*(uint32_t *)value = ARM_CPU(uc, mycpu)->env.cp15.c1_coproc;
|
|
|
|
break;
|
2017-05-14 10:42:49 +03:00
|
|
|
case UC_ARM64_REG_TPIDR_EL0:
|
|
|
|
*(int64_t *)value = ARM_CPU(uc, mycpu)->env.cp15.tpidr_el0;
|
|
|
|
break;
|
|
|
|
case UC_ARM64_REG_TPIDRRO_EL0:
|
|
|
|
*(int64_t *)value = ARM_CPU(uc, mycpu)->env.cp15.tpidrro_el0;
|
|
|
|
break;
|
|
|
|
case UC_ARM64_REG_TPIDR_EL1:
|
|
|
|
*(int64_t *)value = ARM_CPU(uc, mycpu)->env.cp15.tpidr_el1;
|
|
|
|
break;
|
2016-04-04 18:25:30 +03:00
|
|
|
case UC_ARM64_REG_X29:
|
2017-05-14 10:42:49 +03:00
|
|
|
*(int64_t *)value = ARM_CPU(uc, mycpu)->env.xregs[29];
|
|
|
|
break;
|
2016-04-04 18:25:30 +03:00
|
|
|
case UC_ARM64_REG_X30:
|
2017-05-14 10:42:49 +03:00
|
|
|
*(int64_t *)value = ARM_CPU(uc, mycpu)->env.xregs[30];
|
|
|
|
break;
|
2016-04-04 18:25:30 +03:00
|
|
|
case UC_ARM64_REG_PC:
|
2017-05-14 10:42:49 +03:00
|
|
|
*(uint64_t *)value = ARM_CPU(uc, mycpu)->env.pc;
|
|
|
|
break;
|
2016-04-04 18:25:30 +03:00
|
|
|
case UC_ARM64_REG_SP:
|
2017-05-14 10:42:49 +03:00
|
|
|
*(int64_t *)value = ARM_CPU(uc, mycpu)->env.xregs[31];
|
|
|
|
break;
|
2017-03-31 05:21:45 +03:00
|
|
|
case UC_ARM64_REG_NZCV:
|
2017-05-14 10:42:49 +03:00
|
|
|
*(int32_t *)value = cpsr_read(&ARM_CPU(uc, mycpu)->env) & CPSR_NZCV;
|
|
|
|
break;
|
2016-04-04 18:25:30 +03:00
|
|
|
}
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-04 18:25:30 +03:00
|
|
|
int arm64_reg_write(struct uc_struct *uc, unsigned int *regs, void* const* vals, int count)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
2016-09-23 17:38:21 +03:00
|
|
|
CPUState *mycpu = uc->cpu;
|
2016-04-04 18:25:30 +03:00
|
|
|
int i;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2016-04-04 18:25:30 +03:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
unsigned int regid = regs[i];
|
|
|
|
const void *value = vals[i];
|
2017-03-07 16:29:34 +03:00
|
|
|
if (regid >= UC_ARM64_REG_V0 && regid <= UC_ARM64_REG_V31) {
|
|
|
|
regid += UC_ARM64_REG_Q0 - UC_ARM64_REG_V0;
|
|
|
|
}
|
2017-01-15 15:13:35 +03:00
|
|
|
if (regid >= UC_ARM64_REG_X0 && regid <= UC_ARM64_REG_X28) {
|
2016-04-04 18:25:30 +03:00
|
|
|
ARM_CPU(uc, mycpu)->env.xregs[regid - UC_ARM64_REG_X0] = *(uint64_t *)value;
|
2017-01-15 15:13:35 +03:00
|
|
|
} else if (regid >= UC_ARM64_REG_W0 && regid <= UC_ARM64_REG_W30) {
|
|
|
|
WRITE_DWORD(ARM_CPU(uc, mycpu)->env.xregs[regid - UC_ARM64_REG_W0], *(uint32_t *)value);
|
2017-03-07 16:29:34 +03:00
|
|
|
} else if (regid >= UC_ARM64_REG_Q0 && regid <= UC_ARM64_REG_Q31) {
|
|
|
|
float64 *src = (float64*) value;
|
|
|
|
uint32_t reg_index = 2*(regid - UC_ARM64_REG_Q0);
|
|
|
|
ARM_CPU(uc, mycpu)->env.vfp.regs[reg_index] = src[0];
|
|
|
|
ARM_CPU(uc, mycpu)->env.vfp.regs[reg_index+1] = src[1];
|
|
|
|
} else if (regid >= UC_ARM64_REG_D0 && regid <= UC_ARM64_REG_D31) {
|
|
|
|
ARM_CPU(uc, mycpu)->env.vfp.regs[2*(regid - UC_ARM64_REG_D0)] = * (float64*) value;
|
|
|
|
} else if (regid >= UC_ARM64_REG_S0 && regid <= UC_ARM64_REG_S31) {
|
|
|
|
WRITE_DWORD(ARM_CPU(uc, mycpu)->env.vfp.regs[2*(regid - UC_ARM64_REG_S0)], *(int32_t*) value);
|
|
|
|
} else if (regid >= UC_ARM64_REG_H0 && regid <= UC_ARM64_REG_H31) {
|
|
|
|
WRITE_WORD(ARM_CPU(uc, mycpu)->env.vfp.regs[2*(regid - UC_ARM64_REG_H0)], *(int16_t*) value);
|
|
|
|
} else if (regid >= UC_ARM64_REG_B0 && regid <= UC_ARM64_REG_B31) {
|
|
|
|
WRITE_BYTE_L(ARM_CPU(uc, mycpu)->env.vfp.regs[2*(regid - UC_ARM64_REG_B0)], *(int8_t*) value);
|
2017-01-15 15:13:35 +03:00
|
|
|
} else {
|
2016-04-04 18:25:30 +03:00
|
|
|
switch(regid) {
|
|
|
|
default: break;
|
2017-05-02 09:51:19 +03:00
|
|
|
case UC_ARM64_REG_CPACR_EL1:
|
|
|
|
ARM_CPU(uc, mycpu)->env.cp15.c1_coproc = *(uint32_t *)value;
|
|
|
|
break;
|
2017-05-14 10:42:49 +03:00
|
|
|
case UC_ARM64_REG_TPIDR_EL0:
|
|
|
|
ARM_CPU(uc, mycpu)->env.cp15.tpidr_el0 = *(uint64_t *)value;
|
|
|
|
break;
|
|
|
|
case UC_ARM64_REG_TPIDRRO_EL0:
|
|
|
|
ARM_CPU(uc, mycpu)->env.cp15.tpidrro_el0 = *(uint64_t *)value;
|
|
|
|
break;
|
|
|
|
case UC_ARM64_REG_TPIDR_EL1:
|
|
|
|
ARM_CPU(uc, mycpu)->env.cp15.tpidr_el1 = *(uint64_t *)value;
|
|
|
|
break;
|
2016-04-04 18:25:30 +03:00
|
|
|
case UC_ARM64_REG_X29:
|
2017-05-14 10:42:49 +03:00
|
|
|
ARM_CPU(uc, mycpu)->env.xregs[29] = *(uint64_t *)value;
|
|
|
|
break;
|
2016-04-04 18:25:30 +03:00
|
|
|
case UC_ARM64_REG_X30:
|
2017-05-14 10:42:49 +03:00
|
|
|
ARM_CPU(uc, mycpu)->env.xregs[30] = *(uint64_t *)value;
|
|
|
|
break;
|
2016-04-04 18:25:30 +03:00
|
|
|
case UC_ARM64_REG_PC:
|
2017-05-14 10:42:49 +03:00
|
|
|
ARM_CPU(uc, mycpu)->env.pc = *(uint64_t *)value;
|
|
|
|
// force to quit execution and flush TB
|
|
|
|
uc->quit_request = true;
|
|
|
|
uc_emu_stop(uc);
|
|
|
|
break;
|
2016-04-04 18:25:30 +03:00
|
|
|
case UC_ARM64_REG_SP:
|
2017-05-14 10:42:49 +03:00
|
|
|
ARM_CPU(uc, mycpu)->env.xregs[31] = *(uint64_t *)value;
|
|
|
|
break;
|
2017-03-31 05:21:45 +03:00
|
|
|
case UC_ARM64_REG_NZCV:
|
2017-05-14 10:42:49 +03:00
|
|
|
cpsr_write(&ARM_CPU(uc, mycpu)->env, *(uint32_t *) value, CPSR_NZCV);
|
|
|
|
break;
|
2016-04-04 18:25:30 +03:00
|
|
|
}
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-19 14:50:28 +03:00
|
|
|
DEFAULT_VISIBILITY
|
2017-04-24 18:25:30 +03:00
|
|
|
#ifdef TARGET_WORDS_BIGENDIAN
|
|
|
|
void arm64eb_uc_init(struct uc_struct* uc)
|
|
|
|
#else
|
2015-08-21 10:04:50 +03:00
|
|
|
void arm64_uc_init(struct uc_struct* uc)
|
2017-04-24 18:25:30 +03:00
|
|
|
#endif
|
2015-08-21 10:04:50 +03: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-02-01 01:22:20 +03:00
|
|
|
uc->release = arm64_release;
|
2015-08-21 10:04:50 +03:00
|
|
|
uc_common_init(uc);
|
|
|
|
}
|