10#define MAXOS_VIRTUAL_H
15#include <libcommon/string.h>
20namespace MaxOS::processes
60 } virtual_memory_chunk_t;
96 } virtual_memory_region_t;
107 uint64_t* m_pml4_root_address =
nullptr;
108 uint64_t* m_pml4_root_physical_address =
nullptr;
112 size_t m_current_chunk = 0;
113 size_t m_next_available_address;
116 void add_free_chunk(uintptr_t start_address,
size_t size);
122 void fill_up_to_address(uintptr_t address,
size_t flags,
bool mark_used);
128 void*
allocate(
size_t size,
size_t flags);
129 void*
allocate(
size_t requested_size,
size_t& actual_size,
size_t flags);
130 void*
allocate(uint64_t address,
size_t requested_size,
size_t& actual_size,
size_t flags);
131 void free(
void* address,
bool reusable_pages =
true);
Manages the virtual memory of the system and provides functions to allocate and free memory in the vi...
VirtualMemoryManager()
Construct a new Virtual Memory Manager object and set up the initial page tables (kernel mapped into ...
void * load_physical_into_address_space(uintptr_t physical_address, size_t size, size_t flags)
Load physical memory into the VMM's address space.
void free(void *address, bool reusable_pages=true)
Free a chunk of virtual memory.
void * allocate(size_t size, size_t flags)
Allocate a new chunk of virtual memory.
void unload_range_from_process(const void *start_address, size_t size)
Unload a shared range of pages from this vmm.
size_t memory_used()
Returns the amount of memory used.
uint64_t * pml4_root_address_physical()
Get the physical address of the PML4 root.
~VirtualMemoryManager()
Destroy the Virtual Memory Manager object and free all used pages.
uint64_t * pml4_root_address()
Get the virtual address of the PML4 root.
void * load_range_from_process(VirtualMemoryManager *other_vmm, const void *start_address, size_t size)
Loads the pages from a virtual memory region in another vmm into this one.
void * load_shared_memory(const string &name)
Load shared memory into the VMM's address space.
Defines a PhysicalMemoryManager class for managing physical memory allocation and deallocation of pag...
constexpr uint64_t PAGE_SIZE
The size of a page (4KB)
A chunk of memory that has been freed and is available for allocation. A node in a linked list.
size_t size
The size of the chunk's memory region in bytes.
uintptr_t start_address
The first virtual address that this chunk covers.
struct FreeChunk * next
Pointer to the next free chunk in the list (not sequential in memory)
A container for a region of virtual memory that has been allocated by the Virtual Memory Manager.
size_t flags
The flags for the memory (see VirtualFlags)
uintptr_t start_address
The first virtual address that this chunk covers.
size_t size
The size of the chunk's memory region in bytes.
A region of virtual memory containing multiple chunks (should fit in one page).
struct VirtualMemoryRegion * next
Pointer to the next region in the list.
struct PACKED MaxOS::memory::VirtualMemoryRegion virtual_memory_region_t
Alias for VirtualMemoryRegion struct.
VirtualFlags
Flags for chunks used in virtual memory allocation.
@ RESERVE
Reserve the memory but do not map any physical memory to it.
constexpr size_t CHUNKS_PER_PAGE
Number of chunks per virtual memory region (limited to fit in one page)
constexpr size_t VMM_RESERVED
The starting address for the VMM to start allocating from when in higher half (otherwise can cause co...