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

#include <rex/kernel/guest_memory.h>

Public Member Functions

 GuestMemory (const GuestMemory &)=delete
GuestMemory & operator= (const GuestMemory &)=delete
Result< void > CopyFrom (u32 address, std::span< std::byte > out) const
Result< void > CopyTo (u32 address, std::span< const std::byte > data)
Result< std::span< const std::byte > > GetReadOnlySpan (u32 address, u32 length) const
Result< std::span< std::byte > > GetWritableSpan (u32 address, u32 length)
template<typename T>
requires std::is_trivially_copyable_v<T>
Result< T > Load (u32 address) const
template<typename T>
requires std::is_trivially_copyable_v<T>
Result< void > Store (u32 address, const T &value)
Result< std::string > ReadString (u32 address, u32 max_length=4096) const
Result< std::u16string > ReadString16 (u32 address, u32 max_length=4096) const
Result< u32 > Allocate (u32 size, u32 alignment, GuestProtect protect, GuestHeap heap=GuestHeap::kTitle)
Result< void > Free (u32 address)
Result< void > Protect (u32 address, u32 length, GuestProtect protect)
kernel::MemoryView GetView () const
Result< u32 > HostToGuest (const void *host) const

Friends

class TitleRuntime
class kernel::GuestAllocator
class kernel::TitleState

Detailed Description

Checked guest virtual memory, used by everything except recompiled code. A call whose range is not mapped with the needed access returns a kMemory error and changes nothing. Spans stay valid until TitleRuntime::Unload.

Member Function Documentation

◆ Allocate()

Result< u32 > rex::kernel::GuestMemory::Allocate ( u32 size,
u32 alignment,
GuestProtect protect,
GuestHeap heap = GuestHeap::kTitle )
nodiscard

Allocates guest memory.

Parameters
sizeThe size in bytes.
alignmentA power of two, at least 4096 for title memory.
heapThe heap, title memory by default. Physical is the 64 KiB window at 0xA0000000.
Returns
The address, or a kMemory error.

◆ CopyFrom()

Result< void > rex::kernel::GuestMemory::CopyFrom ( u32 address,
std::span< std::byte > out ) const
nodiscard

Copies guest memory into a buffer.

Parameters
outThe buffer to fill. Its size is the length.
Returns
Ok, or a kMemory error.

◆ CopyTo()

Result< void > rex::kernel::GuestMemory::CopyTo ( u32 address,
std::span< const std::byte > data )
nodiscard

Copies bytes into guest memory.

Parameters
dataThe bytes to copy.
Returns
Ok, or a kMemory error when any byte is not mapped writable.

◆ Free()

Result< void > rex::kernel::GuestMemory::Free ( u32 address)
nodiscard

Frees memory from Allocate, from either heap.

Parameters
addressThe address Allocate returned.
Returns
Ok, or a kMemory error.

◆ GetReadOnlySpan()

Result< std::span< const std::byte > > rex::kernel::GuestMemory::GetReadOnlySpan ( u32 address,
u32 length ) const
nodiscard

Returns guest memory to read in place, or a kMemory error.

◆ GetView()

kernel::MemoryView rex::kernel::GuestMemory::GetView ( ) const
nodiscard

Returns an address view usable from any thread until TitleRuntime::Unload.

◆ GetWritableSpan()

Result< std::span< std::byte > > rex::kernel::GuestMemory::GetWritableSpan ( u32 address,
u32 length )
nodiscard

Returns guest memory to write in place, or a kMemory error when any byte is not mapped writable.

◆ HostToGuest()

Result< u32 > rex::kernel::GuestMemory::HostToGuest ( const void * host) const
nodiscard

Translates a host pointer inside guest memory to its guest address.

Parameters
hostA pointer, such as the data of a span from GetWritableSpan.
Returns
The guest address, or a kMemory error when the pointer is outside guest memory.

◆ Load()

template<typename T>
requires std::is_trivially_copyable_v<T>
Result< T > rex::kernel::GuestMemory::Load ( u32 address) const
nodiscard

Reads a guest value.

Template Parameters
TA trivially copyable guest type, such as be_u32 or a struct of be_ fields.
Returns
A copy, or a kMemory error.

Reads a guest value.

Returns
A copy, or a kMemory error.

◆ Protect()

Result< void > rex::kernel::GuestMemory::Protect ( u32 address,
u32 length,
GuestProtect protect )
nodiscard

Changes the access of mapped pages. The range covers every page from the one holding the first byte to the one holding the last.

Returns
Ok, or a kMemory error when any page is not mapped.

◆ ReadString()

Result< std::string > rex::kernel::GuestMemory::ReadString ( u32 address,
u32 max_length = 4096 ) const
nodiscard

Reads a zero terminated 8 bit string.

Parameters
max_lengthThe character limit when no terminator comes first.
Returns
The string, or a kMemory error.

◆ ReadString16()

Result< std::u16string > rex::kernel::GuestMemory::ReadString16 ( u32 address,
u32 max_length = 4096 ) const
nodiscard

Reads a zero terminated big endian UTF-16 string.

Parameters
max_lengthThe character limit when no terminator comes first.
Returns
The string in host byte order, or a kMemory error.

◆ Store()

template<typename T>
requires std::is_trivially_copyable_v<T>
Result< void > rex::kernel::GuestMemory::Store ( u32 address,
const T & value )
nodiscard

Writes a guest value.

Template Parameters
TA trivially copyable guest type, such as be_u32 or a struct of be_ fields.
Returns
Ok, or a kMemory error.

Writes a guest value.

Returns
Ok, or a kMemory error.

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