ReXGlue SDK
Loading...
Searching...
No Matches
rex::kernel::BaseHeap Class Reference
Inheritance diagram for rex::kernel::BaseHeap:
[legend]

Public Member Functions

virtual ~BaseHeap ()
u32 GetHeapBase () const
u32 GetHeapSize () const
u32 GetPageSize () const
HeapType GetHeapType () const
u32 GetHostAddressOffset () const
template<typename T = u8*>
T TranslateRelative (size_t relative_address) const
virtual void Dispose ()
u32 GetTotalPageCount () const
u32 GetUnreservedPageCount () const
u32 GetReservedPageCount () const
u32 GetCommittedPages () const
void PlotProfile () const
virtual bool Alloc (u32 size, u32 alignment, u32 allocation_type, u32 protect, bool top_down, u32 *out_address)
virtual bool AllocFixed (u32 base_address, u32 size, u32 alignment, u32 allocation_type, u32 protect)
virtual bool AllocRange (u32 low_address, u32 high_address, u32 size, u32 alignment, u32 allocation_type, u32 protect, bool top_down, u32 *out_address)
virtual bool AllocSystemHeap (u32 size, u32 alignment, u32 allocation_type, u32 protect, bool top_down, u32 *out_address)
virtual bool Decommit (u32 address, u32 size)
virtual bool Release (u32 address, u32 *out_region_size=nullptr)
bool ReleaseRange (u32 address, u32 size, u32 *out_address, u32 *out_size)
virtual bool Protect (u32 address, u32 size, u32 protect, u32 *old_protect=nullptr)
bool QueryRegionInfo (u32 base_address, HeapAllocation *out_info)
bool QuerySize (u32 address, u32 *out_size)
bool QueryProtect (u32 address, u32 *out_protect)
rex::memory::PageAccess QueryRangeAccess (u32 low_address, u32 high_address)
rex::memory::PageAccess GetHostPageAccess (u32 host_page_number) const
bool ReapplyHostPageAccess (void *host_address, bool is_write)

Protected Member Functions

rex::memory::PageAccess GetHostPageGuestAccess (u32 host_page_number) const
bool SyncHostPageAccess (u32 start_page_number, u32 end_page_number)
virtual std::unique_lock< rex::platform::RankedMutex > AcquireHostPageReconcileLock () const
u32 GetSystemRangeBase () const
bool AllocFixedLocked (u32 base_address, u32 size, u32 alignment, u32 allocation_type, u32 protect)
bool AllocRangeLocked (u32 low_address, u32 high_address, u32 size, u32 alignment, u32 allocation_type, u32 protect, bool top_down, u32 *out_address)
bool DecommitLocked (u32 address, u32 size)
bool ReleaseLocked (u32 address, u32 *out_region_size)
bool ReleaseRangeLocked (u32 address, u32 size, u32 *out_address, u32 *out_size)
bool ProtectLocked (u32 address, u32 size, u32 protect, u32 *old_protect)
bool CheckProtectRangeLocked (u32 address, u32 size, u32 *out_start_page, u32 *out_end_page)
virtual bool IsHostPageWriteWatched (u32 host_page_number) const
void OnPageCommitted (u32 page_number)
void OnPageDecommitted (u32 page_number)
void Initialize (kernel::GuestAddressSpace *memory, u8 *membase, HeapType heap_type, u32 heap_base, u32 heap_size, u32 page_size, u32 host_address_offset=0)

Protected Attributes

kernel::GuestAddressSpace * memory_
u8 * membase_
HeapType heap_type_
u32 heap_base_
u32 heap_size_
u32 page_size_
u32 page_size_shift_ = 0
u32 host_address_offset_
u32 unreserved_page_count_ = 0
std::atomic< u32 > committed_pages_ {0}
const char * profile_pool_ = nullptr
const char * profile_plot_ = nullptr
std::recursive_mutex heap_mutex_
std::vector< PageEntry > page_table_

Friends

class PhysicalHeap

Constructor & Destructor Documentation

◆ ~BaseHeap()

virtual rex::kernel::BaseHeap::~BaseHeap ( )
virtual

Does not release committed pages, which Dispose does.

Member Function Documentation

◆ AcquireHostPageReconcileLock()

virtual std::unique_lock< rex::platform::RankedMutex > rex::kernel::BaseHeap::AcquireHostPageReconcileLock ( ) const
protectedvirtual

Takes the memory watch lock for operations that reconcile host page access.

Returns
The held lock, or an empty lock when guest pages are at least host page sized.

Reimplemented in rex::kernel::PhysicalHeap.

◆ Alloc()

virtual bool rex::kernel::BaseHeap::Alloc ( u32 size,
u32 alignment,
u32 allocation_type,
u32 protect,
bool top_down,
u32 * out_address )
virtual

Allocates pages anywhere in the heap. Fails when not enough contiguous pages are found.

Parameters
allocation_typeMemoryAllocationFlag bits.
protectMemoryProtectFlag bits.
top_downTrue to allocate from the top of the heap.
out_addressReceives the allocated guest address.

Reimplemented in rex::kernel::PhysicalHeap.

◆ AllocFixed()

virtual bool rex::kernel::BaseHeap::AllocFixed ( u32 base_address,
u32 size,
u32 alignment,
u32 allocation_type,
u32 protect )
virtual

Allocates pages at an address. Fails when the pages are allocated.

Parameters
base_addressGuest address to allocate at.
allocation_typeMemoryAllocationFlag bits.
protectMemoryProtectFlag bits.

Reimplemented in rex::kernel::PhysicalHeap.

◆ AllocRange()

virtual bool rex::kernel::BaseHeap::AllocRange ( u32 low_address,
u32 high_address,
u32 size,
u32 alignment,
u32 allocation_type,
u32 protect,
bool top_down,
u32 * out_address )
virtual

Allocates pages within an address range. Fails when not enough contiguous pages are found.

Parameters
allocation_typeMemoryAllocationFlag bits.
protectMemoryProtectFlag bits.
top_downTrue to allocate from the top of the range.
out_addressReceives the allocated guest address.

Reimplemented in rex::kernel::PhysicalHeap.

◆ AllocSystemHeap()

virtual bool rex::kernel::BaseHeap::AllocSystemHeap ( u32 size,
u32 alignment,
u32 allocation_type,
u32 protect,
bool top_down,
u32 * out_address )
virtual

Allocates from the system portion, the top of a virtual heap. A physical heap uses Alloc.

Parameters
allocation_typeMemoryAllocationFlag bits.
protectMemoryProtectFlag bits.
top_downTrue to allocate from the top of the heap.
out_addressReceives the allocated guest address.

Reimplemented in rex::kernel::PhysicalHeap.

◆ CheckProtectRangeLocked()

bool rex::kernel::BaseHeap::CheckProtectRangeLocked ( u32 address,
u32 size,
u32 * out_start_page,
u32 * out_end_page )
protected

Checks that a range can be protected: it is not empty, lies inside the heap, stays in one region and is committed. The caller holds heap_mutex_.

Parameters
addressThe first address of the range.
sizeThe size of the range in bytes.
out_start_pageThe page number holding the first byte.
out_end_pageThe page number holding the last byte.
Returns
True when the range can be protected.

◆ Decommit()

virtual bool rex::kernel::BaseHeap::Decommit ( u32 address,
u32 size )
virtual

Decommits pages in the given range. Partially overlapped pages are decommitted.

Reimplemented in rex::kernel::PhysicalHeap.

◆ Dispose()

virtual void rex::kernel::BaseHeap::Dispose ( )
virtual

Disposes and decommits all memory and clears the page table.

◆ GetHeapBase()

u32 rex::kernel::BaseHeap::GetHeapBase ( ) const
inline

Returns the heap offset relative to membase, without the host_address_offset adjustment.

◆ GetHostAddressOffset()

u32 rex::kernel::BaseHeap::GetHostAddressOffset ( ) const
inline

Returns the offset added to guest addresses to get host addresses, excluding membase.

◆ GetHostPageAccess()

rex::memory::PageAccess rex::kernel::BaseHeap::GetHostPageAccess ( u32 host_page_number) const

Returns the host access for a host page, the union of the committed title protection of its guest pages. A write watched page stays read only. The caller holds the heap mutex and, when guest pages are smaller than host pages, the memory watch lock.

Parameters
host_page_numberHost page number from the heap host base, with GetHostAddressOffset().

◆ GetHostPageGuestAccess()

rex::memory::PageAccess rex::kernel::BaseHeap::GetHostPageGuestAccess ( u32 host_page_number) const
protected

Returns the union of the committed guest protection of the guest pages in a host page, ignoring write watches. The caller holds the same locks as for GetHostPageAccess.

Parameters
host_page_numberHost page number from the heap host base, with GetHostAddressOffset().
Returns
The host access the guest protection allows.

◆ GetSystemRangeBase()

u32 rex::kernel::BaseHeap::GetSystemRangeBase ( ) const
protected

Returns the first address of the range reserved for system allocations, the end of the heap for a heap without a reserved range. Title allocations end one byte below it.

◆ Initialize()

void rex::kernel::BaseHeap::Initialize ( kernel::GuestAddressSpace * memory,
u8 * membase,
HeapType heap_type,
u32 heap_base,
u32 heap_size,
u32 page_size,
u32 host_address_offset = 0 )
protected

Initializes the heap properties and allocates the page table.

Parameters
heap_baseThe heap offset relative to membase.
host_address_offsetThe offset added to guest addresses to give host addresses.

◆ IsHostPageWriteWatched()

virtual bool rex::kernel::BaseHeap::IsHostPageWriteWatched ( u32 host_page_number) const
inlineprotectedvirtual

Reports whether a host page is write watched, so SyncHostPageAccess must not restore write access to it. Only PhysicalHeap tracks this.

Returns
True when the page is watched.

Reimplemented in rex::kernel::PhysicalHeap.

◆ OnPageCommitted()

void rex::kernel::BaseHeap::OnPageCommitted ( u32 page_number)
protected

Counts a page as committed and reports it to the profiler memory pool of the heap.

◆ OnPageDecommitted()

void rex::kernel::BaseHeap::OnPageDecommitted ( u32 page_number)
protected

Counts a page as decommitted and reports it to the profiler memory pool of the heap.

◆ PlotProfile()

void rex::kernel::BaseHeap::PlotProfile ( ) const

Plots the committed bytes on the profiler graph. Does nothing for a heap that views another.

◆ Protect()

virtual bool rex::kernel::BaseHeap::Protect ( u32 address,
u32 size,
u32 protect,
u32 * old_protect = nullptr )
virtual

Modifies the protection mode of pages within the given range.

Parameters
protectMemoryProtectFlag bits.
old_protectReceives the previous protection.

Reimplemented in rex::kernel::PhysicalHeap.

◆ QueryProtect()

bool rex::kernel::BaseHeap::QueryProtect ( u32 address,
u32 * out_protect )

Returns the current protection mode of the region containing the given address.

Parameters
out_protectReceives the MemoryProtectFlag bits.

◆ QueryRangeAccess()

rex::memory::PageAccess rex::kernel::BaseHeap::QueryRangeAccess ( u32 low_address,
u32 high_address )

Returns the strictest readability and writability for the range.

◆ QueryRegionInfo()

bool rex::kernel::BaseHeap::QueryRegionInfo ( u32 base_address,
HeapAllocation * out_info )

Returns information about the given region of pages.

Parameters
out_infoReceives the region information.

◆ QuerySize()

bool rex::kernel::BaseHeap::QuerySize ( u32 address,
u32 * out_size )

Returns the size of the region containing the given address.

Parameters
out_sizeReceives the size in bytes.

◆ ReapplyHostPageAccess()

bool rex::kernel::BaseHeap::ReapplyHostPageAccess ( void * host_address,
bool is_write )

Reapplies the host access the heap grants to a faulting host page, under the heap mutex. The caller holds the memory watch lock.

Parameters
is_writeTrue for a write, false for a read.
Returns
True when the heap allows the access and the host page now permits it.

◆ Release()

virtual bool rex::kernel::BaseHeap::Release ( u32 address,
u32 * out_region_size = nullptr )
virtual

Decommits and releases pages in the given range. Partially overlapped pages are released.

Parameters
out_region_sizeReceives the size of the released region.

Reimplemented in rex::kernel::PhysicalHeap.

◆ ReleaseRange()

bool rex::kernel::BaseHeap::ReleaseRange ( u32 address,
u32 size,
u32 * out_address,
u32 * out_size )

Releases the pages of a range inside one region, trimming or splitting it. Partially overlapped pages are released. Only for a heap whose regions need no parent heap.

Parameters
sizeSize of the range in bytes, not 0.
out_addressReceives the guest address of the first released page.
out_sizeReceives the size of the released pages in bytes.
Returns
True on success, false when the range is not all inside one allocated region.

◆ SyncHostPageAccess()

bool rex::kernel::BaseHeap::SyncHostPageAccess ( u32 start_page_number,
u32 end_page_number )
protected

Reconciles host page access with guest page protection. The caller holds the memory watch lock when guest pages are smaller than host pages.

Parameters
start_page_numberFirst guest page of the range.
end_page_numberLast guest page of the range.
Returns
True when the host protection was updated.

◆ TranslateRelative()

template<typename T = u8*>
T rex::kernel::BaseHeap::TranslateRelative ( size_t relative_address) const
inline

Translates an offset from the heap base to a host pointer.

Member Data Documentation

◆ committed_pages_

std::atomic<u32> rex::kernel::BaseHeap::committed_pages_ {0}
protected

Number of committed pages, changed with the heap mutex held.

◆ heap_mutex_

std::recursive_mutex rex::kernel::BaseHeap::heap_mutex_
protected

Guards the page table and the counters of the heap.

◆ host_address_offset_

u32 rex::kernel::BaseHeap::host_address_offset_
protected

Offset added to guest addresses to give host addresses, given to Initialize.

◆ memory_

kernel::GuestAddressSpace* rex::kernel::BaseHeap::memory_
protected

Guest address space that owns the heap, source of the host page size.

◆ page_size_shift_

u32 rex::kernel::BaseHeap::page_size_shift_ = 0
protected

Base 2 logarithm of page_size_, shifts a heap offset into a page number.

◆ profile_pool_

const char* rex::kernel::BaseHeap::profile_pool_ = nullptr
protected

Profiler memory pool of the heap, null for a heap that views the memory of another heap.


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