unicorn/qemu/target-arm/unicorn_arm.c

133 lines
3.5 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 "hw/boards.h"
#include "hw/arm/arm.h"
#include "sysemu/cpus.h"
#include "unicorn.h"
#include "cpu.h"
#include "unicorn_common.h"
#include "uc_priv.h"
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;
}
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;
env = first_cpu->env_ptr;
memset(env->regs, 0, sizeof(env->regs));
env->pc = 0;
}
2015-08-26 10:30:58 +00:00
int arm_reg_read(struct uc_struct *uc, unsigned int regid, void *value)
2015-08-21 07:04:50 +00:00
{
CPUState *mycpu;
mycpu = first_cpu;
2016-01-23 01:56:47 +00:00
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];
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;
}
2015-08-26 10:30:58 +00:00
int arm_reg_write(struct uc_struct *uc, unsigned int regid, const void *value)
2015-08-21 07:04:50 +00:00
{
2015-08-26 10:30:58 +00:00
CPUState *mycpu = first_cpu;
2015-08-21 07:04:50 +00:00
2016-01-23 01:56:47 +00:00
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;
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:
ARM_CPU(uc, mycpu)->env.pc = *(uint32_t *)value;
2016-01-23 01:56:47 +00:00
ARM_CPU(uc, mycpu)->env.regs[15] = *(uint32_t *)value;
// force to quit execution and flush TB
uc->quit_request = true;
uc_emu_stop(uc);
2016-01-23 01:56:47 +00:00
break;
2016-01-23 01:08:49 +00:00
}
2015-08-21 07:04:50 +00:00
}
return 0;
}
static bool arm_stop_interrupt(int intno)
{
switch(intno) {
default:
return false;
case EXCP_UDEF:
return true;
}
}
static uc_err arm_query(struct uc_struct *uc, uc_query_type type, size_t *result)
{
CPUState *mycpu = first_cpu;
uint32_t mode;
switch(type) {
case UC_QUERY_MODE:
// 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;
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;
uc->stop_interrupt = arm_stop_interrupt;
uc->query = arm_query;
2015-08-21 07:04:50 +00:00
uc_common_init(uc);
}