ReXGlue SDK
Loading...
Searching...
No Matches
rex::kernel::GuestAddressSpace Class Reference

#include <rex/kernel/heap.h>

Inheritance diagram for rex::kernel::GuestAddressSpace:
[legend]

Public Types

using PhysicalMemoryInvalidationCallback

Public Member Functions

 GuestAddressSpace ()
bool Initialize ()
const std::filesystem::path & GetFileName () const
u8 * GetVirtualMembase () const
rex::memory::GuestMembase GetMembase () const
template<typename T = u8*>
T TranslateVirtual (u32 guest_address) const
template<typename T = u8*>
T TryTranslateVirtual (u32 guest_address, u32 required=kMemoryProtectRead) const
u8 * GetPhysicalMembase () const
template<typename T = u8*>
T TranslatePhysical (u32 guest_address) const
u32 HostToGuestVirtual (const void *host_address) const
u32 GetPhysicalAddress (u32 address) const
void Zero (u32 address, u32 size) const
void Fill (u32 address, u32 size, u8 value) const
void Copy (u32 dest, u32 src, u32 size) const
AccessViolationResult HandleAccessViolation (void *host_address, bool is_write)
void * RegisterPhysicalMemoryInvalidationCallback (PhysicalMemoryInvalidationCallback callback, void *callback_context)
void UnregisterPhysicalMemoryInvalidationCallback (void *callback_handle)
void EnablePhysicalMemoryAccessCallbacks (u32 physical_address, u32 length, bool enable_invalidation_notifications, bool enable_data_providers)
u32 SystemHeapAlloc (u32 size, u32 alignment=0x20, u32 system_heap_flags=kSystemHeapDefault) override
void SystemHeapFree (u32 address, u32 *out_region_size=nullptr) override
u32 AllocPhysical (u32 size, u32 page_size, u32 protect, u32 min_address, u32 max_address, u32 alignment) override
bool IsAddressMapped (u32 address) const override
const BaseHeap * LookupHeap (u32 address) const
BaseHeap * LookupHeap (u32 address)
BaseHeap * LookupHeapByType (bool physical, u32 page_size)
void GetHeapsPageStats (const BaseHeap *const *provided_heaps, size_t heaps_count, u32 &unreserved_pages, u32 &reserved_pages, u32 &used_pages, u32 &reserved_bytes)
VirtualHeap * GetPhysicalHeap ()
void SetBudget (u32 megabytes)
u32 GetTitlePages () const
u32 GetBudgetPages () const
void PlotProfile ()
bool InitializeFunctionTable (u32 code_base, u32 code_size, u32 image_base, u32 image_size)
bool DestroyFunctionTable (u32 code_base)
bool SetFunction (u32 guest_address, PPCFunc *host_function)
std::unique_lock< rex::platform::RankedMutex > LockWatches ()

Friends

class BaseHeap
class GuestAllocator
class WorkspaceHeap
class PhysicalHeap

Detailed Description

The guest address space, backed by a memory mapped file at a fixed host address. It is split into ranges, each a BaseHeap that is a VirtualHeap or a PhysicalHeap.

Member Typedef Documentation

◆ PhysicalMemoryInvalidationCallback

Initial value:
std::pair<u32, u32> (*)(void* context_ptr,
u32 physical_address_start,
u32 length, bool exact_range)

Callback for physical memory invalidation notifications. Callbacks are one shot, may fire spuriously or more than once, and fire only for accesses through the guest virtual views of physical memory, so a handler ignores pages it does not track and data providers write through the host physical heap. A triggered page is unwatched in one guest view until requested again. The granularity is 1 page, so a handler invalidates all data in every page from the one holding the first written byte to the one holding the last. A write fault unprotects the shortest range the callbacks return.

Parameters
context_ptrThe context given at registration.
lengthThe length of the watched region in bytes.
exact_rangeTrue when only the exact range is being unwatched.
Returns
The start and length of the smallest physical memory region around the watched region that can be unwatched, or (0, UINT32_MAX) when it does not matter.

Constructor & Destructor Documentation

◆ GuestAddressSpace()

rex::kernel::GuestAddressSpace::GuestAddressSpace ( )

Constructs the memory system and registers it as the active instance.

Member Function Documentation

◆ AllocPhysical()

u32 rex::kernel::GuestAddressSpace::AllocPhysical ( u32 size,
u32 page_size,
u32 protect,
u32 min_address,
u32 max_address,
u32 alignment )
overridevirtual

Allocates contiguous physical memory for the title. SystemHeapFree releases it.

Parameters
sizeThe size in bytes, rounded up to the page size.
page_sizeThe page size in bytes: 4 KiB, 64 KiB or 16 MiB.
protectMemoryProtectFlag bits.
min_addressThe lowest acceptable physical address.
max_addressThe highest acceptable physical address.
alignmentThe alignment in bytes, rounded up to the page size.
Returns
The base address, or 0 when no memory is available.

Implements rex::kernel::ISystemHeap.

◆ Copy()

void rex::kernel::GuestAddressSpace::Copy ( u32 dest,
u32 src,
u32 size ) const

Copies a non overlapping range of guest memory, like memcpy.

◆ DestroyFunctionTable()

bool rex::kernel::GuestAddressSpace::DestroyFunctionTable ( u32 code_base)

Removes the function table of a module and releases its pages.

Parameters
code_baseThe first guest address of the code, as given to InitializeFunctionTable.
Returns
False when no table is registered for code_base.

◆ EnablePhysicalMemoryAccessCallbacks()

void rex::kernel::GuestAddressSpace::EnablePhysicalMemoryAccessCallbacks ( u32 physical_address,
u32 length,
bool enable_invalidation_notifications,
bool enable_data_providers )

Enables physical memory access callbacks for a memory range, snapped to system page boundaries.

◆ GetBudgetPages()

u32 rex::kernel::GuestAddressSpace::GetBudgetPages ( ) const
nodiscard
Returns
The budget in 4 KB units, 0 when uncapped.

◆ GetFileName()

const std::filesystem::path & rex::kernel::GuestAddressSpace::GetFileName ( ) const
inline

Returns the path of the memory mapped file that backs all memory.

◆ GetHeapsPageStats()

void rex::kernel::GuestAddressSpace::GetHeapsPageStats ( const BaseHeap *const * provided_heaps,
size_t heaps_count,
u32 & unreserved_pages,
u32 & reserved_pages,
u32 & used_pages,
u32 & reserved_bytes )
Parameters
unreserved_pagesReceives the unreserved page count.
reserved_pagesReceives the reserved page count.
used_pagesReceives the used page count.
reserved_bytesReceives the reserved size in bytes.

◆ GetMembase()

rex::memory::GuestMembase rex::kernel::GuestAddressSpace::GetMembase ( ) const
inline

Returns the translator for guest virtual addresses, which does not depend on this object.

◆ GetPhysicalAddress()

u32 rex::kernel::GuestAddressSpace::GetPhysicalAddress ( u32 address) const

Returns the guest physical address for a guest virtual address, or UINT32_MAX when it cannot be obtained.

◆ GetTitlePages()

u32 rex::kernel::GuestAddressSpace::GetTitlePages ( ) const
nodiscard

Returns the title pages counted against the budget. The count is in 4 KB units.

◆ HandleAccessViolation()

AccessViolationResult rex::kernel::GuestAddressSpace::HandleAccessViolation ( void * host_address,
bool is_write )

Resolves a host access violation inside guest memory.

Parameters
host_addressThe host address the access faulted on.
is_writeTrue when the access was a write.
Returns
kMMIOMiss for a register block, kResumed when the write watch resolved the fault, otherwise kUnhandled.

◆ HostToGuestVirtual()

u32 rex::kernel::GuestAddressSpace::HostToGuestVirtual ( const void * host_address) const

Translates a host address to a guest virtual address.

◆ Initialize()

bool rex::kernel::GuestAddressSpace::Initialize ( )

Initializes the memory system.

Returns
False when the host address space cannot be reserved or the file mapping fails.

◆ InitializeFunctionTable()

bool rex::kernel::GuestAddressSpace::InitializeFunctionTable ( u32 code_base,
u32 code_size,
u32 image_base,
u32 image_size )

Initializes the per module function dispatch table at IMAGE_BASE + IMAGE_SIZE, indexed by (guest_addr - code_base) * 2. The function table calls are internally locked and may be made from any thread.

Parameters
code_baseThe first guest address of the code.

◆ IsAddressMapped()

bool rex::kernel::GuestAddressSpace::IsAddressMapped ( u32 address) const
inlineoverridevirtual

◆ LockWatches()

std::unique_lock< rex::platform::RankedMutex > rex::kernel::GuestAddressSpace::LockWatches ( )
inlinenodiscard

Takes the lock that guards the physical memory watch state of the heaps and of every registered invalidation callback client. It is a Leaf lock held while callbacks run, so callbacks take no other ranked lock.

◆ PlotProfile()

void rex::kernel::GuestAddressSpace::PlotProfile ( )

Plots heap, allocator pool and title memory use on the profiler graphs. Call it once per frame.

◆ RegisterPhysicalMemoryInvalidationCallback()

void * rex::kernel::GuestAddressSpace::RegisterPhysicalMemoryInvalidationCallback ( PhysicalMemoryInvalidationCallback callback,
void * callback_context )

Registers a physical memory invalidation callback.

Returns
A handle for unregistering.

◆ SetBudget()

void rex::kernel::GuestAddressSpace::SetBudget ( u32 megabytes)

Sets the title memory budget.

Parameters
megabytesBudget in MiB, 0 for no cap.

◆ SetFunction()

bool rex::kernel::GuestAddressSpace::SetFunction ( u32 guest_address,
PPCFunc * host_function )

Sets the host function for a guest address.

Returns
False when guest_address is outside every registered module range.

◆ SystemHeapAlloc()

u32 rex::kernel::GuestAddressSpace::SystemHeapAlloc ( u32 size,
u32 alignment = 0x20,
u32 system_heap_flags = kSystemHeapDefault )
overridevirtual

Allocates virtual memory from the system heap, kept apart from title memory but reachable with normal guest virtual addresses.

Parameters
system_heap_flagsSystemHeapFlag bits.

Implements rex::kernel::ISystemHeap.

◆ SystemHeapFree()

void rex::kernel::GuestAddressSpace::SystemHeapFree ( u32 address,
u32 * out_region_size = nullptr )
overridevirtual

Frees memory allocated with SystemHeapAlloc.

Parameters
out_region_sizeReceives the size of the freed region.

Implements rex::kernel::ISystemHeap.

◆ TranslatePhysical()

template<typename T = u8*>
T rex::kernel::GuestAddressSpace::TranslatePhysical ( u32 guest_address) const
inline

Translates a guest physical address to a host pointer. The contents are big endian.

◆ TranslateVirtual()

template<typename T = u8*>
T rex::kernel::GuestAddressSpace::TranslateVirtual ( u32 guest_address) const
inline

Translates a guest virtual address to a host address, with the arithmetic recompiled code uses. The contents at the host address are big endian.

◆ TryTranslateVirtual()

template<typename T = u8*>
T rex::kernel::GuestAddressSpace::TryTranslateVirtual ( u32 guest_address,
u32 required = kMemoryProtectRead ) const
inline

Translates a guest virtual address to a host pointer only when the title could access it.

Parameters
requiredThe kMemoryProtect bits the page must grant, the read bit by default.
Returns
The host pointer, null when the address is 0, misaligned, in no heap, or on a page that lacks a required bit.

◆ UnregisterPhysicalMemoryInvalidationCallback()

void rex::kernel::GuestAddressSpace::UnregisterPhysicalMemoryInvalidationCallback ( void * callback_handle)

Unregisters a callback added with RegisterPhysicalMemoryInvalidationCallback.

Parameters
callback_handleThe handle returned at registration.

The documentation for this class was generated from the following file: