Public Types | Public Member Functions
bfn::unique_map_ptr_x64< T > Class Template Reference

Public Types

using pointer = T *
 
using integer_pointer = uintptr_t
 
using size_type = size_t
 
using element_type = T
 

Public Member Functions

 unique_map_ptr_x64 ()
 
 unique_map_ptr_x64 (std::nullptr_t donotcare)
 
 unique_map_ptr_x64 (integer_pointer virt, size_type size)
 
 unique_map_ptr_x64 (integer_pointer vmap, integer_pointer phys, x64::memory_attr::attr_type attr)
 
 unique_map_ptr_x64 (integer_pointer vmap, const std::vector< std::pair< integer_pointer, size_type >> &list, x64::memory_attr::attr_type attr)
 
 unique_map_ptr_x64 (integer_pointer vmap, integer_pointer virt, integer_pointer cr3, size_type size, x64::msrs::value_type pat)
 
 unique_map_ptr_x64 (unique_map_ptr_x64 &&other) noexcept
 
virtual ~unique_map_ptr_x64 () noexcept
 
unique_map_ptr_x64operator= (unique_map_ptr_x64 &&other) noexcept
 
unique_map_ptr_x64operator= (std::nullptr_t dontcare) noexcept
 
std::add_lvalue_reference< T >::type operator* () const
 
auto operator-> () const noexcept
 
virtual pointer get () const noexcept
 
 operator bool () const noexcept
 
virtual size_type size () const noexcept
 
auto release () noexcept
 
void reset (pointer ptr=pointer(), size_type size=size_type(), size_type unaligned_size=size_type()) noexcept
 
void reset (const std::tuple< pointer, size_type, size_type > &p) noexcept
 
void swap (unique_map_ptr_x64 &other) noexcept
 
void flush () noexcept
 
void cache_flush () noexcept
 
 unique_map_ptr_x64 (const unique_map_ptr_x64 &)=delete
 
unique_map_ptr_x64operator= (const unique_map_ptr_x64 &)=delete
 

Detailed Description

template<class T>
class bfn::unique_map_ptr_x64< T >

Unique Map

Like std::unique_ptr, unique_map_ptr_x64 is a smart map that owns and manages the mapping between virtual and physical memory. Memory is mapped when the unique_map_ptr_x64 is first created, and unmapped when the unique_map_ptr_x64 is destroyed.

Although this class can be used directly, it should be created using make_unique_map_x64, which allocates the virtual memory for you as shown in this example:

Example:

std::cout << bfn::make_unique_map_x64<char>(phys) << '\n';

Unlike std::unique_pointer, unique_map_ptr_x64 takes additional arguments and doesn't support an array syntax. It should also be noted that this class provides some additional helpers specific to a map including a way to get it's size, as well as a means to flush TLB entries associated with this map if needed (although when the map is created, the local TLB is flushed for you, and thus this should only be needed if you share this map with another core)

Definition at line 54 of file map_ptr_x64.h.

Member Typedef Documentation

◆ pointer

template<class T>
using bfn::unique_map_ptr_x64< T >::pointer = T*

Definition at line 343 of file map_ptr_x64.h.

◆ integer_pointer

template<class T>
using bfn::unique_map_ptr_x64< T >::integer_pointer = uintptr_t

Definition at line 344 of file map_ptr_x64.h.

◆ size_type

template<class T>
using bfn::unique_map_ptr_x64< T >::size_type = size_t

Definition at line 345 of file map_ptr_x64.h.

◆ element_type

template<class T>
using bfn::unique_map_ptr_x64< T >::element_type = T

Definition at line 346 of file map_ptr_x64.h.

Constructor & Destructor Documentation

◆ unique_map_ptr_x64() [1/8]

template<class T>
bfn::unique_map_ptr_x64< T >::unique_map_ptr_x64 ( )
inline

Default Map

This constructor can be used to create a default map that maps to nothing

Definition at line 353 of file map_ptr_x64.h.

◆ unique_map_ptr_x64() [2/8]

template<class T>
bfn::unique_map_ptr_x64< T >::unique_map_ptr_x64 ( std::nullptr_t  donotcare)
inline

Invalid Map

This constructor can be used to create an invalid map that maps to nothing

Definition at line 364 of file map_ptr_x64.h.

◆ unique_map_ptr_x64() [3/8]

template<class T>
bfn::unique_map_ptr_x64< T >::unique_map_ptr_x64 ( integer_pointer  virt,
size_type  size 
)
inline

Release Map

This constructor can be used to create a map that maps to an exist virtual address and size. Note that this should be used with case as the original map must be released. Otherwise you will have two owners.

Definition at line 377 of file map_ptr_x64.h.

◆ unique_map_ptr_x64() [4/8]

template<class T>
bfn::unique_map_ptr_x64< T >::unique_map_ptr_x64 ( integer_pointer  vmap,
integer_pointer  phys,
x64::memory_attr::attr_type  attr 
)
inline

Map Single Page

This constructor can be used to map a single virtual memory page to a single physical memory page.

Example:

std::cout << bfn::make_unique_map_x64<char>(phys) << '\n';
Precondition
expects: vmap != 0
expects: vmap & (x64::page_size - 1) == 0
expects: phys != 0
expects: phys & (x64::page_size - 1) == 0
expects: attr != 0
Postcondition
ensures: get() != nullptr
Parameters
vmapthe virtual address to map the physical address to
physthe physical address to map
attrdefines how to map the memory. Defaults to map_read_write

Definition at line 404 of file map_ptr_x64.h.

◆ unique_map_ptr_x64() [5/8]

template<class T>
bfn::unique_map_ptr_x64< T >::unique_map_ptr_x64 ( integer_pointer  vmap,
const std::vector< std::pair< integer_pointer, size_type >> &  list,
x64::memory_attr::attr_type  attr 
)
inline

Map Physically Contiguous / Non-Contiguous Range

This constructor can be used to map both physically contiguous, and physically non-contiguous memory by providing a list of physical pages to map. The list consists of std::pairs, each containing a physical address, and a size. A physically contiguous memory range would consist of a list of one std::pair contains the physical address and it's size. A physically non-contiguous range would consist of a list of each page range that makes up the memory to be mapped (similar to a Windows MDL). In either case the total number of bytes mapped is equal to the total of each size field in each std::pair in the list provided.

Note
the resulting virtual memory address, like the other constructors, will contain the lower bits of the physical address so that you can not only get a map, but also receive a map somewhere inside of the page if needed.
this function doesn't check to make sure that the physical ranges you provide don't overlap as the mapping will succeed either way, so unless you want the same physical page being mapped to different parts of your virtual range, make sure you don't have overlapping ranges. In some cases you might want that, the best example being ring buffers.

Example:

auto phys_range_1 = std::make_pair(phys1, size1);
auto phys_range_2 = std::make_pair(phys2, size2);
auto phys_range_3 = std::make_pair(phys3, size3);
auto list = {phys_range_1, phys_range_2, phys_range_3};
std::cout << bfn::make_unique_map_x64<char>(list) << '\n';
Precondition
expects: vmap != 0
expects: vmap & (x64::page_size - 1) == 0
expects: list.empty() == false
expects: list.at(i).first != 0
expects: list.at(i).second != 0
expects: list.at(i).second & (x64::page_size - 1) == 0
expects: attr != 0
Postcondition
ensures: get() != nullptr
Parameters
vmapthe virtual address to map the physical address to
listlist of std::pairs, each containing a physical address and a size, defining a physical address range to add to the virtual address mapping
attrdefines how to map the memory. Defaults to map_read_write

Definition at line 469 of file map_ptr_x64.h.

◆ unique_map_ptr_x64() [6/8]

template<class T>
bfn::unique_map_ptr_x64< T >::unique_map_ptr_x64 ( integer_pointer  vmap,
integer_pointer  virt,
integer_pointer  cr3,
size_type  size,
x64::msrs::value_type  pat 
)
inline

Map Physically Contiguous / Non-Contiguous Range With CR3

This constructor can be used to map both physically contiguous, and physically non-contiguous memory by providing an existing virtually contiguous memory range address and size, as well as the CR3 value that defines the existing virtual to physical memory mappings. This is useful when mapping guest memory into VMM, and caution should be taken if mapping executable memory.

Note
since this function must map in the guest's page tables to locate each physical address for each page being mapped, this function is very expensive, and should not be used in time critical operations.

Example:

std::cout << bfn::make_unique_map_x64<char>(virt, vmcs::guest_cr3::get(), size) << '\n';
Precondition
expects: vmap != 0
expects: vmap & (x64::page_size - 1) == 0
expects: virt != 0
expects: cr3 != 0
expects: cr3 & (x64::page_size - 1) == 0
expects: size != 0
expects: attr != 0
Postcondition
ensures: get() != nullptr
Parameters
vmapthe virtual address to map the range to
virtthe virtual address containing the existing mapping
cr3the root page table containing the existing virtual to physical memory mappings
sizethe number of bytes to map
patthe pat msr associated with the provided cr3

Definition at line 542 of file map_ptr_x64.h.

◆ unique_map_ptr_x64() [7/8]

template<class T>
bfn::unique_map_ptr_x64< T >::unique_map_ptr_x64 ( unique_map_ptr_x64< T > &&  other)
inlinenoexcept

Move Constructor

Like std::unique_ptr, this is equivalent to

Example:

reset(other.release());

The unique_map_ptr_x64 provided will no longer be valid, and the new unique_map_ptr_x64 will have the mapping provided. Note that this should be a fast operation, and no mapping / unmapping occurs. If the existing mapping is invalid, or already unmapped, the resulting unique_map_ptr_x64 will also be invalid / unmapped.

Precondition
expects: none
Postcondition
ensures: none
Parameters
otherthe unique_map_ptr_x64 to move

Definition at line 579 of file map_ptr_x64.h.

◆ ~unique_map_ptr_x64()

template<class T>
virtual bfn::unique_map_ptr_x64< T >::~unique_map_ptr_x64 ( )
inlinevirtualnoexcept

Destructor

Unmaps any existing map this unique_map_ptr_x64 holds. Note that if an occurs while attempting to unmap, exceptions are caught and execution continues. If this occurs, the results are undefined.

Precondition
expects: none
Postcondition
ensures: none

Definition at line 594 of file map_ptr_x64.h.

◆ unique_map_ptr_x64() [8/8]

template<class T>
bfn::unique_map_ptr_x64< T >::unique_map_ptr_x64 ( const unique_map_ptr_x64< T > &  )
delete

Member Function Documentation

◆ operator=() [1/3]

template<class T>
unique_map_ptr_x64& bfn::unique_map_ptr_x64< T >::operator= ( unique_map_ptr_x64< T > &&  other)
inlinenoexcept

Copy Operator

Like std::unique_ptr, this is equivalent to

Example:

reset(other.release());

The unique_map_ptr_x64 provided will no longer be valid, and the new unique_map_ptr_x64 will have the mapping provided. Note that this should be a fast operation, and no mapping / unmapping occurs. If the existing mapping is invalid, or already unmapped, the resulting unique_map_ptr_x64 will also be invalid / unmapped.

Precondition
expects: none
Postcondition
ensures: none
Parameters
otherthe unique_map_ptr_x64 to copy
Returns
reference to this

Definition at line 625 of file map_ptr_x64.h.

◆ operator=() [2/3]

template<class T>
unique_map_ptr_x64& bfn::unique_map_ptr_x64< T >::operator= ( std::nullptr_t  dontcare)
inlinenoexcept

Copy Operator (reset)

Like std::unique_ptr, this is equivalent to

Example:

The result of this operation is the current unique_map_ptr_x64 will be unmapped and invalid.

Precondition
expects: none
Postcondition
ensures: none
Parameters
dontcarenullptr
Returns
reference to this

Definition at line 649 of file map_ptr_x64.h.

◆ operator*()

template<class T>
std::add_lvalue_reference<T>::type bfn::unique_map_ptr_x64< T >::operator* ( ) const
inline

Dereference

Returns *T. Note that if the map is invalid, this operation will likely segfault.

Precondition
expects: none
Postcondition
ensures: none
Returns
*T

Definition at line 667 of file map_ptr_x64.h.

◆ operator->()

template<class T>
auto bfn::unique_map_ptr_x64< T >::operator-> ( ) const
inlinenoexcept

Dereference

Returns *T. Note that if the map is invalid, this operation will likely segfault.

Precondition
expects: none
Postcondition
ensures: none
Returns
*T

Definition at line 680 of file map_ptr_x64.h.

◆ get()

template<class T>
virtual pointer bfn::unique_map_ptr_x64< T >::get ( ) const
inlinevirtualnoexcept

Get *T

Returns *T. Note that if the map is invalid, any use of the result will likely segfault.

Precondition
expects: none
Postcondition
ensures: none
Returns
*T

Definition at line 693 of file map_ptr_x64.h.

◆ operator bool()

template<class T>
bfn::unique_map_ptr_x64< T >::operator bool ( ) const
inlinenoexcept

Check Validity

Precondition
expects: none
Postcondition
ensures: none
Returns
returns true if the map is valid, false otherwise

Definition at line 703 of file map_ptr_x64.h.

◆ size()

template<class T>
virtual size_type bfn::unique_map_ptr_x64< T >::size ( ) const
inlinevirtualnoexcept

Size

Precondition
expects: none
Postcondition
ensures: none
Returns
returns the size of the map in bytes. Returns 0 if the map is invalid

Definition at line 714 of file map_ptr_x64.h.

◆ release()

template<class T>
auto bfn::unique_map_ptr_x64< T >::release ( )
inlinenoexcept

Release

Like std::unique_ptr, this releases the map from this unique_map_ptr_x64 and returns a std::tuple containing the virtual address and size of the map. It is left to the user of this function to either deliver the std::tuple to another unique_map_ptr_x64 via reset(), or manually unmap / free the virtual address

Note
use with caution as this is an unsafe operation
Precondition
expects: none
Postcondition
ensures: none
Returns
returns a std::tuple containing the virtual address and size of the map. The user must manually unmap / free this memory

Definition at line 733 of file map_ptr_x64.h.

◆ reset() [1/2]

template<class T>
void bfn::unique_map_ptr_x64< T >::reset ( pointer  ptr = pointer(),
size_type  size = size_type(),
size_type  unaligned_size = size_type() 
)
inlinenoexcept

Reset

Like std::unique_ptr, this resets the unique_map_ptr_x64. If no args are provide, this function unmaps / frees the unique_map_ptr_x64 and the mapped memory becomes invalid. If a valid virtual address and size are provided, the current unique_map_ptr_x64 is unmapped and freed, and the newly provided virtual address and size are used in it's place.

Note
use with caution as this is an unsafe operation
Precondition
expects: none
Postcondition
ensures: none
Parameters
ptrpointer to virtual memory to use. Defaults to nullptr
sizethe size of the virtual memory provided in bytes. Defaults to 0
unaligned_sizethe unaligned size of the virtual memory provided in bytes. Defaults to 0. In most cases this is the same thing as size, but if your using a map from CR3, and the virtual address is not page aligned, you must add lower(virt)

Definition at line 768 of file map_ptr_x64.h.

◆ reset() [2/2]

template<class T>
void bfn::unique_map_ptr_x64< T >::reset ( const std::tuple< pointer, size_type, size_type > &  p)
inlinenoexcept

Reset

Like std::unique_ptr, this resets the unique_map_ptr_x64. If no args are provide, this function unmaps / frees the unique_map_ptr_x64 and the mapped memory becomes invalid. If a valid virtual address and size are provided, the current unique_map_ptr_x64 is unmapped and freed, and the newly provided virtual address and size are used in it's place.

Note
use with caution as this is an unsafe operation
Precondition
expects: none
Postcondition
ensures: none
Parameters
pstd::tuple containing the virtual memory address and size in bytes of the new mapping to use.

Definition at line 797 of file map_ptr_x64.h.

◆ swap()

template<class T>
void bfn::unique_map_ptr_x64< T >::swap ( unique_map_ptr_x64< T > &  other)
inlinenoexcept

Swap

Swaps the mappings of one unique_map_ptr_x64 with another

Precondition
expects: none
Postcondition
ensures: none
Parameters
otherthe unique_map_ptr_x64 to swap with

Definition at line 809 of file map_ptr_x64.h.

◆ flush()

template<class T>
void bfn::unique_map_ptr_x64< T >::flush ( )
inlinenoexcept

Flush

Flushes the TLB entries associated with the virtual address ranges this unique_map_ptr_x64 holds. This is done automatically when mapping memory, but might be needed if this map is shared with another core whose TLB has not been properly flushed.

Precondition
expects: none
Postcondition
ensures: none

Definition at line 826 of file map_ptr_x64.h.

◆ cache_flush()

template<class T>
void bfn::unique_map_ptr_x64< T >::cache_flush ( )
inlinenoexcept

Cache Flush

Flushes the Cache associated with the virtual address ranges this unique_map_ptr_x64 holds. This is done automatically when unmapping memory, but might be needed manually by users

Precondition
expects: none
Postcondition
ensures: none

Definition at line 842 of file map_ptr_x64.h.

◆ operator=() [3/3]

template<class T>
unique_map_ptr_x64& bfn::unique_map_ptr_x64< T >::operator= ( const unique_map_ptr_x64< T > &  )
delete

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