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"
|
|
|
|
|
|
|
|
|
|
|
|
#define READ_QWORD(x) ((uint64)x)
|
|
|
|
#define READ_DWORD(x) (x & 0xffffffff)
|
|
|
|
#define READ_WORD(x) (x & 0xffff)
|
|
|
|
#define READ_BYTE_H(x) ((x & 0xffff) >> 8)
|
|
|
|
#define READ_BYTE_L(x) (x & 0xff)
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
TCGContext *s = (TCGContext *) ctx;
|
|
|
|
|
|
|
|
g_free(s->tb_ctx.tbs);
|
|
|
|
struct uc_struct* uc = s->uc;
|
|
|
|
ARMCPU* cpu = (ARMCPU*) uc->cpu;
|
|
|
|
g_free(cpu->cpreg_indexes);
|
|
|
|
g_free(cpu->cpreg_values);
|
|
|
|
g_free(cpu->cpreg_vmstate_indexes);
|
|
|
|
g_free(cpu->cpreg_vmstate_values);
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2015-08-26 13:30:58 +03:00
|
|
|
CPUArchState *env = first_cpu->env_ptr;
|
2015-08-21 10:04:50 +03:00
|
|
|
memset(env->xregs, 0, sizeof(env->xregs));
|
|
|
|
|
|
|
|
env->pc = 0;
|
|
|
|
}
|
|
|
|
|
2015-08-26 13:30:58 +03:00
|
|
|
int arm64_reg_read(struct uc_struct *uc, unsigned int regid, void *value)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
2015-08-26 13:30:58 +03:00
|
|
|
CPUState *mycpu = first_cpu;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2015-08-24 07:36:33 +03:00
|
|
|
if (regid >= UC_ARM64_REG_X0 && regid <= UC_ARM64_REG_X28)
|
|
|
|
*(int64_t *)value = ARM_CPU(uc, mycpu)->env.xregs[regid - UC_ARM64_REG_X0];
|
2015-08-21 10:04:50 +03:00
|
|
|
else {
|
|
|
|
switch(regid) {
|
|
|
|
default: break;
|
2015-08-24 07:36:33 +03:00
|
|
|
case UC_ARM64_REG_X29:
|
2015-08-21 10:04:50 +03:00
|
|
|
*(int64_t *)value = ARM_CPU(uc, mycpu)->env.xregs[29];
|
|
|
|
break;
|
2015-08-24 07:36:33 +03:00
|
|
|
case UC_ARM64_REG_X30:
|
2015-08-21 10:04:50 +03:00
|
|
|
*(int64_t *)value = ARM_CPU(uc, mycpu)->env.xregs[30];
|
|
|
|
break;
|
2015-08-24 07:36:33 +03:00
|
|
|
case UC_ARM64_REG_PC:
|
2015-08-21 10:04:50 +03:00
|
|
|
*(uint64_t *)value = ARM_CPU(uc, mycpu)->env.pc;
|
|
|
|
break;
|
2015-09-08 03:40:42 +03:00
|
|
|
case UC_ARM64_REG_SP:
|
|
|
|
*(int64_t *)value = ARM_CPU(uc, mycpu)->env.xregs[31];
|
|
|
|
break;
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define WRITE_DWORD(x, w) (x = (x & ~0xffffffff) | (w & 0xffffffff))
|
|
|
|
#define WRITE_WORD(x, w) (x = (x & ~0xffff) | (w & 0xffff))
|
|
|
|
#define WRITE_BYTE_H(x, b) (x = (x & ~0xff00) | (b & 0xff))
|
|
|
|
#define WRITE_BYTE_L(x, b) (x = (x & ~0xff) | (b & 0xff))
|
|
|
|
|
2015-08-26 13:30:58 +03:00
|
|
|
int arm64_reg_write(struct uc_struct *uc, unsigned int regid, const void *value)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
2015-08-26 13:30:58 +03:00
|
|
|
CPUState *mycpu = first_cpu;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2015-08-24 07:36:33 +03:00
|
|
|
if (regid >= UC_ARM64_REG_X0 && regid <= UC_ARM64_REG_X28)
|
2015-09-04 06:17:08 +03:00
|
|
|
ARM_CPU(uc, mycpu)->env.xregs[regid - UC_ARM64_REG_X0] = *(uint64_t *)value;
|
2015-08-21 10:04:50 +03:00
|
|
|
else {
|
|
|
|
switch(regid) {
|
|
|
|
default: break;
|
2015-08-24 07:36:33 +03:00
|
|
|
case UC_ARM64_REG_X29:
|
2015-09-04 06:17:08 +03:00
|
|
|
ARM_CPU(uc, mycpu)->env.xregs[29] = *(uint64_t *)value;
|
2015-08-21 10:04:50 +03:00
|
|
|
break;
|
2015-08-24 07:36:33 +03:00
|
|
|
case UC_ARM64_REG_X30:
|
2015-09-04 06:17:08 +03:00
|
|
|
ARM_CPU(uc, mycpu)->env.xregs[30] = *(uint64_t *)value;
|
2015-08-21 10:04:50 +03:00
|
|
|
break;
|
2015-08-24 07:36:33 +03:00
|
|
|
case UC_ARM64_REG_PC:
|
2015-08-21 10:04:50 +03:00
|
|
|
ARM_CPU(uc, mycpu)->env.pc = *(uint64_t *)value;
|
|
|
|
break;
|
2015-09-08 03:40:42 +03:00
|
|
|
case UC_ARM64_REG_SP:
|
|
|
|
ARM_CPU(uc, mycpu)->env.xregs[31] = *(uint64_t *)value;
|
|
|
|
break;
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__ ((visibility ("default")))
|
|
|
|
void arm64_uc_init(struct uc_struct* uc)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|