mirror of
https://github.com/momo5502/emulator.git
synced 2026-01-28 07:21:02 +00:00
prepare 32bit support with name and class inheritance modifications
This commit is contained in:
66
src/emulator/arch_emulator.hpp
Normal file
66
src/emulator/arch_emulator.hpp
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
Design notes:
|
||||
|
||||
1. emulator: the root interface (provides CPU, memory, and hook interfaces).
|
||||
2. typed_emulator<Traits>: a template that adapts to architecture/bitness via the Traits struct.
|
||||
3. arch_emulator<Traits>: a thin layer for architecture-specific logic, things that are shared by all x86 (32/64), or
|
||||
all ARM (32/64), etc.
|
||||
X. x86_emulator<Traits>: x86_emulator<Traits> are specialisations for
|
||||
x86 and ARM, parameterised by their respective traits (e.g., x86_64_traits) and stuff :)
|
||||
|
||||
1. emulator (cpu_interface, memory_interface, hook_interface)
|
||||
2. └── typed_emulator<address_t, register_t, ...>
|
||||
3. └── arch_emulator<arch_traits>
|
||||
├── x86_emulator<x86_32_traits>
|
||||
├── x86_emulator<x86_64_traits>
|
||||
├── arm_emulator<arm_32_traits>
|
||||
└── arm_emulator<arm_64_traits>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "typed_emulator.hpp"
|
||||
#include "x86_register.hpp"
|
||||
|
||||
// --[Core]--------------------------------------------------------------------------
|
||||
|
||||
template <typename Traits>
|
||||
struct arch_emulator : typed_emulator<Traits>
|
||||
{
|
||||
};
|
||||
|
||||
template <typename Traits>
|
||||
struct x86_emulator : arch_emulator<Traits>
|
||||
{
|
||||
using register_type = typename Traits::register_type;
|
||||
using pointer_type = typename Traits::pointer_type;
|
||||
|
||||
virtual void set_segment_base(register_type base, pointer_type value) = 0;
|
||||
virtual void load_gdt(pointer_type address, uint32_t limit) = 0;
|
||||
};
|
||||
|
||||
template <typename Traits>
|
||||
struct arm_emulator : arch_emulator<Traits>
|
||||
{
|
||||
};
|
||||
|
||||
enum class x86_hookable_instructions
|
||||
{
|
||||
invalid,
|
||||
syscall,
|
||||
cpuid,
|
||||
rdtsc,
|
||||
rdtscp,
|
||||
};
|
||||
|
||||
// --[x86_64]-------------------------------------------------------------------------
|
||||
|
||||
struct x86_64_traits
|
||||
{
|
||||
using pointer_type = uint64_t;
|
||||
using register_type = x86_register;
|
||||
static constexpr register_type instruction_pointer = x86_register::rip;
|
||||
static constexpr register_type stack_pointer = x86_register::rsp;
|
||||
using hookable_instructions = x86_hookable_instructions;
|
||||
};
|
||||
|
||||
using x86_64_emulator = x86_emulator<x86_64_traits>;
|
||||
@@ -2,18 +2,17 @@
|
||||
|
||||
#include "emulator.hpp"
|
||||
|
||||
template <typename PointerType, typename Register, Register InstructionPointer, Register StackPointer,
|
||||
typename HookableInstructions>
|
||||
template <typename Traits>
|
||||
class typed_emulator : public emulator
|
||||
{
|
||||
public:
|
||||
using registers = Register;
|
||||
using pointer_type = PointerType;
|
||||
using hookable_instructions = HookableInstructions;
|
||||
using registers = typename Traits::register_type;
|
||||
using pointer_type = typename Traits::pointer_type;
|
||||
using hookable_instructions = typename Traits::hookable_instructions;
|
||||
|
||||
static constexpr size_t pointer_size = sizeof(pointer_type);
|
||||
static constexpr registers stack_pointer = StackPointer;
|
||||
static constexpr registers instruction_pointer = InstructionPointer;
|
||||
static constexpr registers stack_pointer = Traits::stack_pointer;
|
||||
static constexpr registers instruction_pointer = Traits::instruction_pointer;
|
||||
|
||||
size_t write_register(registers reg, const void* value, const size_t size)
|
||||
{
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
#pragma once
|
||||
#include "typed_emulator.hpp"
|
||||
#include "x64_register.hpp"
|
||||
|
||||
enum class x64_hookable_instructions
|
||||
{
|
||||
invalid,
|
||||
syscall,
|
||||
cpuid,
|
||||
rdtsc,
|
||||
rdtscp,
|
||||
};
|
||||
|
||||
struct x64_emulator
|
||||
: typed_emulator<uint64_t, x64_register, x64_register::rip, x64_register::rsp, x64_hookable_instructions>
|
||||
{
|
||||
virtual void set_segment_base(x64_register base, pointer_type value) = 0;
|
||||
virtual void load_gdt(pointer_type address, uint32_t limit) = 0;
|
||||
};
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
enum class x64_register
|
||||
// x86_64 and x86_32 register definitions
|
||||
enum class x86_register
|
||||
{
|
||||
invalid = 0,
|
||||
ah,
|
||||
Reference in New Issue
Block a user