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>
This commit is contained in:
Ronald Caesar 2025-08-13 15:00:48 -04:00
parent f15417802d
commit 55af4bebda
2 changed files with 57 additions and 1 deletions

16
core/aarch64/isa.cpp Executable file → Normal file
View file

@ -1,9 +1,25 @@
#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;