2015-08-21 07:04:50 +00: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 03:43:02 +00:00
|
|
|
#include "uc_priv.h"
|
2015-08-21 07:04:50 +00:00
|
|
|
|
|
|
|
|
2016-08-20 11:14:07 +00:00
|
|
|
const int ARM_REGS_STORAGE_SIZE = offsetof(CPUARMState, tlb_table);
|
|
|
|
|
2015-08-21 07:04:50 +00:00
|
|
|
static void arm_set_pc(struct uc_struct *uc, uint64_t address)
|
|
|
|
{
|
|
|
|
((CPUARMState *)uc->current_cpu->env_ptr)->pc = address;
|
|
|
|
((CPUARMState *)uc->current_cpu->env_ptr)->regs[15] = address;
|
|
|
|
}
|
|
|
|
|
2016-01-31 22:22:20 +00:00
|
|
|
void arm_release(void* ctx);
|
|
|
|
|
|
|
|
void arm_release(void* ctx)
|
|
|
|
{
|
|
|
|
TCGContext *s = (TCGContext *) ctx;
|
|
|
|
|
2016-12-21 14:28:36 +00:00
|
|
|
g_free(s->tb_ctx.tbs);
|
2016-01-31 22:22:20 +00:00
|
|
|
struct uc_struct* uc = s->uc;
|
|
|
|
ARMCPU* cpu = (ARMCPU*) uc->cpu;
|
2016-12-21 14:28:36 +00:00
|
|
|
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 arm_reg_reset(struct uc_struct *uc)
|
2015-08-21 07:04:50 +00:00
|
|
|
{
|
2015-08-26 10:30:58 +00:00
|
|
|
(void)uc;
|
2015-08-21 07:04:50 +00:00
|
|
|
CPUArchState *env;
|
|
|
|
|
2016-09-23 14:38:21 +00:00
|
|
|
env = uc->cpu->env_ptr;
|
2015-08-21 07:04:50 +00:00
|
|
|
memset(env->regs, 0, sizeof(env->regs));
|
|
|
|
|
|
|
|
env->pc = 0;
|
|
|
|
}
|
|
|
|
|
2016-04-04 15:25:30 +00:00
|
|
|
int arm_reg_read(struct uc_struct *uc, unsigned int *regs, void **vals, int count)
|
2015-08-21 07:04:50 +00:00
|
|
|
{
|
|
|
|
CPUState *mycpu;
|
2016-04-04 15:25:30 +00:00
|
|
|
int i;
|
2015-08-21 07:04:50 +00:00
|
|
|
|
2016-09-23 14:38:21 +00:00
|
|
|
mycpu = uc->cpu;
|
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];
|
|
|
|
if (regid >= UC_ARM_REG_R0 && regid <= UC_ARM_REG_R12)
|
|
|
|
*(int32_t *)value = ARM_CPU(uc, mycpu)->env.regs[regid - UC_ARM_REG_R0];
|
2016-05-04 08:23:32 +00:00
|
|
|
else if (regid >= UC_ARM_REG_D0 && regid <= UC_ARM_REG_D31)
|
|
|
|
*(float64 *)value = ARM_CPU(uc, mycpu)->env.vfp.regs[regid - UC_ARM_REG_D0];
|
2016-04-04 15:25:30 +00:00
|
|
|
else {
|
|
|
|
switch(regid) {
|
|
|
|
case UC_ARM_REG_CPSR:
|
|
|
|
*(int32_t *)value = cpsr_read(&ARM_CPU(uc, mycpu)->env);
|
|
|
|
break;
|
|
|
|
//case UC_ARM_REG_SP:
|
|
|
|
case UC_ARM_REG_R13:
|
|
|
|
*(int32_t *)value = ARM_CPU(uc, mycpu)->env.regs[13];
|
|
|
|
break;
|
|
|
|
//case UC_ARM_REG_LR:
|
|
|
|
case UC_ARM_REG_R14:
|
|
|
|
*(int32_t *)value = ARM_CPU(uc, mycpu)->env.regs[14];
|
|
|
|
break;
|
|
|
|
//case UC_ARM_REG_PC:
|
|
|
|
case UC_ARM_REG_R15:
|
|
|
|
*(int32_t *)value = ARM_CPU(uc, mycpu)->env.regs[15];
|
|
|
|
break;
|
|
|
|
}
|
2016-01-23 01:08:49 +00:00
|
|
|
}
|
2015-08-21 07:04:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-04 15:25:30 +00:00
|
|
|
int arm_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;
|
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_ARM_REG_R0 && regid <= UC_ARM_REG_R12)
|
|
|
|
ARM_CPU(uc, mycpu)->env.regs[regid - UC_ARM_REG_R0] = *(uint32_t *)value;
|
2016-05-04 08:23:32 +00:00
|
|
|
else if (regid >= UC_ARM_REG_D0 && regid <= UC_ARM_REG_D31)
|
|
|
|
ARM_CPU(uc, mycpu)->env.vfp.regs[regid - UC_ARM_REG_D0] = *(float64 *)value;
|
2016-04-04 15:25:30 +00:00
|
|
|
else {
|
|
|
|
switch(regid) {
|
|
|
|
//case UC_ARM_REG_SP:
|
|
|
|
case UC_ARM_REG_R13:
|
|
|
|
ARM_CPU(uc, mycpu)->env.regs[13] = *(uint32_t *)value;
|
|
|
|
break;
|
|
|
|
//case UC_ARM_REG_LR:
|
|
|
|
case UC_ARM_REG_R14:
|
|
|
|
ARM_CPU(uc, mycpu)->env.regs[14] = *(uint32_t *)value;
|
|
|
|
break;
|
|
|
|
//case UC_ARM_REG_PC:
|
|
|
|
case UC_ARM_REG_R15:
|
2016-06-17 11:46:34 +00:00
|
|
|
ARM_CPU(uc, mycpu)->env.pc = (*(uint32_t *)value & ~1);
|
|
|
|
ARM_CPU(uc, mycpu)->env.thumb = (*(uint32_t *)value & 1);
|
2016-07-30 05:21:44 +00:00
|
|
|
ARM_CPU(uc, mycpu)->env.uc->thumb = (*(uint32_t *)value & 1);
|
2016-06-17 11:46:34 +00:00
|
|
|
ARM_CPU(uc, mycpu)->env.regs[15] = (*(uint32_t *)value & ~1);
|
2016-04-04 15:25:30 +00:00
|
|
|
// force to quit execution and flush TB
|
|
|
|
uc->quit_request = true;
|
|
|
|
uc_emu_stop(uc);
|
2016-01-28 08:03:19 +00:00
|
|
|
|
2016-04-04 15:25:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-01-23 01:08:49 +00:00
|
|
|
}
|
2015-08-21 07:04:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-07 16:43:09 +00:00
|
|
|
static bool arm_stop_interrupt(int intno)
|
|
|
|
{
|
|
|
|
switch(intno) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case EXCP_UDEF:
|
2016-04-20 04:04:15 +00:00
|
|
|
case EXCP_YIELD:
|
2015-09-07 16:43:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 09:14:44 +00:00
|
|
|
static uc_err arm_query(struct uc_struct *uc, uc_query_type type, size_t *result)
|
|
|
|
{
|
2016-09-23 14:38:21 +00:00
|
|
|
CPUState *mycpu = uc->cpu;
|
2016-02-06 01:47:57 +00:00
|
|
|
uint32_t mode;
|
2016-01-23 09:14:44 +00:00
|
|
|
|
|
|
|
switch(type) {
|
2016-01-23 17:08:23 +00:00
|
|
|
case UC_QUERY_MODE:
|
2016-02-06 01:47:57 +00:00
|
|
|
// zero out ARM/THUMB mode
|
|
|
|
mode = uc->mode & ~(UC_MODE_ARM | UC_MODE_THUMB);
|
|
|
|
// THUMB mode or ARM MOde
|
|
|
|
mode += ((ARM_CPU(uc, mycpu)->env.thumb != 0)? UC_MODE_THUMB : UC_MODE_ARM);
|
|
|
|
*result = mode;
|
2016-01-23 09:14:44 +00:00
|
|
|
return UC_ERR_OK;
|
|
|
|
default:
|
|
|
|
return UC_ERR_ARG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-21 07:04:50 +00:00
|
|
|
void arm_uc_init(struct uc_struct* uc)
|
|
|
|
{
|
|
|
|
register_accel_types(uc);
|
|
|
|
arm_cpu_register_types(uc);
|
|
|
|
tosa_machine_init(uc);
|
|
|
|
uc->reg_read = arm_reg_read;
|
|
|
|
uc->reg_write = arm_reg_write;
|
|
|
|
uc->reg_reset = arm_reg_reset;
|
|
|
|
uc->set_pc = arm_set_pc;
|
2015-09-07 16:43:09 +00:00
|
|
|
uc->stop_interrupt = arm_stop_interrupt;
|
2016-01-31 22:22:20 +00:00
|
|
|
uc->release = arm_release;
|
2016-01-23 09:14:44 +00:00
|
|
|
uc->query = arm_query;
|
2015-08-21 07:04:50 +00:00
|
|
|
uc_common_init(uc);
|
|
|
|
}
|