Add CPU class

This commit is contained in:
Starlet Leonhart 2019-06-30 12:42:55 -04:00
parent 39b591bcfd
commit 75c608fd25
7 changed files with 84 additions and 40 deletions

View File

@ -1,5 +1,39 @@
#include "cpu.h"
// TODO: Handle Unicorn errors
namespace core {
Cpu::Cpu() {
uc_open(UC_ARCH_ARM64, UC_MODE_ARM, &uc);
uc_hook hook{};
uc_hook_add(uc, &hook, UC_HOOK_INTR, (void*)HookInterrupt, this, 0, -1);
}
void Cpu::Run(uint64_t address) {
uc_emu_start(uc, address, 1ULL << 63, 0, 0);
}
uint64_t Cpu::GetRegister(uint32_t regid) {
uint64_t registerValue;
uc_reg_read(uc, regid, &registerValue);
return registerValue;
}
void Cpu::SetRegister(uint32_t regid, uint64_t value) {
uc_reg_write(uc, regid, &value);
}
void Cpu::HookInterrupt(uc_engine *uc, uint32_t intno, void *user_data) {
if (intno == 2) {
uint32_t instr{};
uc_mem_read(uc, GetRegister(UC_ARM64_REG_PC) - 4, &instr, 4);
uint32_t svcId = instr >> 5 & 0xFF;
// TODO: Handle SVCs
} else {
syslog(LOG_ERR, "Unhandled interrupt #%i", intno);
uc_close(uc);
}
}
}

View File

@ -1,10 +1,20 @@
#pragma once
#include <syslog.h>
#include <unicorn/unicorn.h>
namespace core {
class Cpu {
private:
public:
Cpu();
~Cpu() { uc_close(uc); };
void Run(uint64_t address);
uint64_t GetRegister(uint32_t regid);
void SetRegister(uint32_t regid, uint64_t value);
uc_engine *uc;
private:
void HookInterrupt(uc_engine *uc, uint32_t intno, void *user_data);
};
}

View File

@ -3,19 +3,17 @@
#include <vector>
#include "memory.h"
namespace core::mem {
std::vector<MemoryRegion> memRegions;
namespace core::memory {
std::vector<MemoryRegion> memoryRegions;
bool Map(uc_engine* uc, uint64_t address, size_t size, std::string label) {
void* ptr = mmap((void*)(address), size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, 0, 0);
if(!ptr)
return false;
if (!ptr) return false;
// Skipping this until the CPU implementation is working
if(uc) {
if (uc) {
uc_err err = uc_mem_map_ptr(uc, address, size, UC_PROT_ALL, ptr);
if(err)
{
if (err) {
syslog(LOG_ERR, "Memory map failed: %s", uc_strerror(err));
return false;
}
@ -23,20 +21,21 @@ namespace core::mem {
syslog(LOG_INFO, "Successfully mapped region '%s' to 0x%x", label.c_str(), address);
memRegions.push_back({label, address, size, ptr});
memoryRegions.push_back({label, address, size, ptr});
return true;
}
// TODO: Boundary checks
void Write(void* data, uint64_t offset, size_t size) { std::memcpy((void*)(offset), data, size); }
void WriteU8(uint8_t value, uint64_t offset) { Write(reinterpret_cast<void*>(&value), offset, 1); }
void WriteU8 (uint8_t value, uint64_t offset) { Write(reinterpret_cast<void*>(&value), offset, 1); }
void WriteU16(uint16_t value, uint64_t offset) { Write(reinterpret_cast<void*>(&value), offset, 2); }
void WriteU32(uint32_t value, uint64_t offset) { Write(reinterpret_cast<void*>(&value), offset, 4); }
void WriteU64(uint64_t value, uint64_t offset) { Write(reinterpret_cast<void*>(&value), offset, 8); }
void Read(void* destination, uint64_t offset, size_t size) { std::memcpy(destination, (void*)(offset), size); }
uint8_t ReadU8(uint64_t offset) { uint8_t value; Read(reinterpret_cast<void*>(&value), offset, 1); return value; }
uint8_t ReadU8 (uint64_t offset) { uint8_t value; Read(reinterpret_cast<void*>(&value), offset, 1); return value; }
uint16_t ReadU16(uint64_t offset) { uint16_t value; Read(reinterpret_cast<void*>(&value), offset, 2); return value; }
uint32_t ReadU32(uint64_t offset) { uint32_t value; Read(reinterpret_cast<void*>(&value), offset, 4); return value; }
uint64_t ReadU64(uint64_t offset) { uint64_t value; Read(reinterpret_cast<void*>(&value), offset, 8); return value; }

View File

@ -2,9 +2,9 @@
#include <string>
#include <unicorn/unicorn.h>
#define MEM_BASE 0x80000000
#define BASE_ADDRESS 0x80000000
namespace core::mem {
namespace core::memory {
struct MemoryRegion {
std::string label;
uint64_t address;
@ -12,16 +12,18 @@ namespace core::mem {
void* ptr;
};
bool Map(uc_engine* uc, uint64_t address, size_t size, std::string label="");
bool Map(uc_engine* uc, uint64_t address, size_t size, std::string label = {});
void Write(void* data, uint64_t offset, size_t size);
void WriteU8(uint8_t value, uint64_t offset);
void WriteU8 (uint8_t value, uint64_t offset);
void WriteU16(uint16_t value, uint64_t offset);
void WriteU32(uint32_t value, uint64_t offset);
void WriteU64(uint64_t value, uint64_t offset);
void Read(void* destination, uint64_t offset, size_t size);
uint8_t ReadU8(uint64_t offset);
uint8_t ReadU8 (uint64_t offset);
uint16_t ReadU16(uint64_t offset);
uint32_t ReadU32(uint64_t offset);
uint64_t ReadU64(uint64_t offset);

View File

View File

@ -19,35 +19,34 @@ namespace core::loader {
bool LoadNro(std::string file) {
syslog(LOG_INFO, "Loading NRO file %s\n", file.c_str());
NroHeader h;
ReadDataFromFile(file, reinterpret_cast<char *>(&h), 0x0, sizeof(NroHeader));
if (h.magic != 0x304F524E) {
syslog(LOG_ERR, "Invalid NRO magic 0x%x\n", h.magic);
NroHeader header;
ReadDataFromFile(file, reinterpret_cast<char *>(&header), 0x0, sizeof(NroHeader));
if (header.magic != 0x304F524E) {
syslog(LOG_ERR, "Invalid NRO magic 0x%x\n", header.magic);
return false;
}
std::vector<uint32_t> text, ro, data;
text.resize(h.segments[0].size);
ro.resize(h.segments[1].size);
data.resize(h.segments[2].size);
text.resize(header.segments[0].size);
ro.resize (header.segments[1].size);
data.resize(header.segments[2].size);
ReadDataFromFile(file, reinterpret_cast<char *>(text.data()), h.segments[0].fileOffset, h.segments[0].size);
ReadDataFromFile(file, reinterpret_cast<char *>(ro.data()), h.segments[1].fileOffset, h.segments[1].size);
ReadDataFromFile(file, reinterpret_cast<char *>(data.data()), h.segments[2].fileOffset, h.segments[2].size);
ReadDataFromFile(file, reinterpret_cast<char *>(text.data()), header.segments[0].fileOffset, header.segments[0].size);
ReadDataFromFile(file, reinterpret_cast<char *>(ro.data()), header.segments[1].fileOffset, header.segments[1].size);
ReadDataFromFile(file, reinterpret_cast<char *>(data.data()), header.segments[2].fileOffset, header.segments[2].size);
if( !mem::Map(nullptr, MEM_BASE, h.segments[0].size, ".text") ||
!mem::Map(nullptr, MEM_BASE + h.segments[0].size, h.segments[1].size, ".ro") ||
!mem::Map(nullptr, MEM_BASE + h.segments[0].size + h.segments[1].size, h.segments[2].size, ".data") ||
!mem::Map(nullptr, MEM_BASE + h.segments[0].size + h.segments[1].size + h.segments[2].size, h.bssSize, ".bss")) {
if( !memory::Map(nullptr, BASE_ADDRESS, header.segments[0].size, ".text") ||
!memory::Map(nullptr, BASE_ADDRESS + header.segments[0].size, header.segments[1].size, ".ro") ||
!memory::Map(nullptr, BASE_ADDRESS + header.segments[0].size + header.segments[1].size, header.segments[2].size, ".data") ||
!memory::Map(nullptr, BASE_ADDRESS + header.segments[0].size + header.segments[1].size + header.segments[2].size, header.bssSize, ".bss")) {
syslog(LOG_ERR, "Failed mapping regions for executable");
return false;
}
mem::Write(text.data(), MEM_BASE, text.size());
mem::Write(ro.data(), MEM_BASE + h.segments[0].size, ro.size());
mem::Write(data.data(), MEM_BASE + h.segments[0].size + h.segments[1].size, data.size());
memory::Write(text.data(), BASE_ADDRESS, text.size());
memory::Write(ro.data(), BASE_ADDRESS + header.segments[0].size, ro.size());
memory::Write(data.data(), BASE_ADDRESS + header.segments[0].size + header.segments[1].size, data.size());
return true;
}
}

View File

@ -681,7 +681,7 @@ uc_err uc_context_alloc(uc_engine *uc, uc_context **context);
/*
Free the memory allocated by uc_context_alloc & uc_mem_regions.
@mem: memory allocated by uc_context_alloc (returned in *context), or
@memory: memory allocated by uc_context_alloc (returned in *context), or
by uc_mem_regions (returned in *regions)
@return UC_ERR_OK on success, or other value on failure (refer to uc_err enum