pound-emu_pound/core/aarch64/isa.cpp
Ronald Caesar 55af4bebda aarch64/mem: Add fast GPA-to-HVA translation helper
In emulating guests with a simple, flat memory model, we frequently need
to translate a guest physical address (GPA) into a host virtual address
(HVA). This is a hot path operation that must be efficient as possible.

This commit introduces gpa_to_hva(), a static inline helper function
designed for this purpose. The implementation relies on the fundamental
pre-condition that the guest's physical RAM is backed by a single,
contiguous region of host virtual memory (typically acquired via mmap).
It treats the GPA not as a pointer but as a direct byte offset from the
base of this host mapping.

This approach is optimal for performance for two key reasons:

1. The translation is a single pointer-offset calculation, which
   typically compiles to a single LEA intruction on x86-64.

2. It preserves memory access  locality. When a guest performs
   sequential accesses, the host's accesses are also sequential,
   allowing the host CPU's hardware prefetcher to function effectively.

This helper provides the fast path for simple RAM accesses. More
complex address spaces involving discontiguous memory or MMIO regions
will require a slower, lookup-based translation mechanism. This
function is not intended for those cases.

Signed-off-by: Ronald Caesar <github43132@proton.me>
2025-08-13 15:19:17 -04:00

36 lines
1.1 KiB
C++

#include "isa.h"
#include "Base/Assert.h"
#include "memory/arena.h"
static inline void* aarch64::memory::gpa_to_hva(aarch64::memory::guest_memory_t* memory, uint64_t gpa)
{
ASSERT(nullptr != memory);
ASSERT(nullptr != memory->base);
ASSERT(gpa < memory->size);
void* hva = memory->base + gpa;
return hva;
}
void cpuTest()
{
aarch64::vcpu_state_t vcpu_states[CPU_CORES] = {};
memory::arena_t guest_memory_arena = memory::arena_init(GUEST_RAM_SIZE);
ASSERT(nullptr != guest_memory_arena.data);
aarch64::memory::guest_memory_t guest_ram = {};
guest_ram.base = guest_memory_arena.data;
guest_ram.size = guest_memory_arena.capacity;
// Outdated Code
CPU cpu;
cpu.pc = 0;
// Simple ARMv8 program in memory (MOVZ X0, #5; ADD X0, X0, #3; RET)
// These are placeholders; real encoding will be parsed later
cpu.write_byte(0, 0x05); // MOVZ placeholder
cpu.write_byte(4, 0x03); // ADD placeholder
cpu.write_byte(8, 0xFF); // RET placeholder
LOG_INFO(ARM, "{}", cpu.read_byte(0));
cpu.print_debug_information();
}