Max OS 0.3
Loading...
Searching...
No Matches
virtual.h
Go to the documentation of this file.
1
9#ifndef MAXOS_VIRTUAL_H
10#define MAXOS_VIRTUAL_H
11
12#include <stdint.h>
13#include <stddef.h>
14
15#include <libcommon/string.h>
16
17#include <memory/physical.h>
18
19
20namespace MaxOS::processes
21{
22 class Process;
23}
24
25namespace MaxOS {
26 namespace memory {
27
38 typedef enum VirtualFlags {
39
40 RESERVE = (1 << 9),
41 SHARED = (1 << 10),
42
43 } virtual_flags_t;
44
54 typedef struct VirtualMemoryChunk {
55
56 uintptr_t start_address;
57 size_t size;
58 size_t flags;
59
60 } virtual_memory_chunk_t;
61
71 typedef struct FreeChunk {
72
73 uintptr_t start_address;
74 size_t size;
75 struct FreeChunk* next;
76
77 } free_chunk_t;
78
79 constexpr size_t CHUNKS_PER_PAGE = PAGE_SIZE / sizeof(virtual_memory_chunk_t) - 1;
80 constexpr size_t VMM_RESERVED = 0x138000000;
81
91 typedef struct PACKED VirtualMemoryRegion {
92
95
96 } virtual_memory_region_t;
97
105
106 private:
107 uint64_t* m_pml4_root_address = nullptr;
108 uint64_t* m_pml4_root_physical_address = nullptr;
109
110 virtual_memory_region_t* m_first_region = nullptr;
111 virtual_memory_region_t* m_current_region = nullptr;
112 size_t m_current_chunk = 0;
113 size_t m_next_available_address;
114
115 free_chunk_t* m_free_chunks = nullptr;
116 void add_free_chunk(uintptr_t start_address, size_t size);
117
118 free_chunk_t* find_and_remove_free_chunk(size_t size);
119 virtual_memory_chunk_t* find_containing_chunk(const void* start_address, size_t size);
120
121 void new_region();
122 void fill_up_to_address(uintptr_t address, size_t flags, bool mark_used);
123
124 public:
127
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);
132
133 void* load_physical_into_address_space(uintptr_t physical_address, size_t size, size_t flags);
134
135 void* load_range_from_process(VirtualMemoryManager* other_vmm, const void* start_address, size_t size);
136 void unload_range_from_process(const void* start_address, size_t size);
137
138 void* load_shared_memory(const string &name);
139 void* load_shared_memory(uintptr_t physical_address, size_t size);
140
141 uint64_t* pml4_root_address();
142 uint64_t* pml4_root_address_physical();
143
144 size_t memory_used();
145
146 };
147 }
148}
149
150
151#endif // MAXOS_VIRTUAL_H
Manages the virtual memory of the system and provides functions to allocate and free memory in the vi...
Definition virtual.h:104
VirtualMemoryManager()
Construct a new Virtual Memory Manager object and set up the initial page tables (kernel mapped into ...
Definition virtual.cpp:25
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.
Definition virtual.cpp:524
void free(void *address, bool reusable_pages=true)
Free a chunk of virtual memory.
Definition virtual.cpp:282
void * allocate(size_t size, size_t flags)
Allocate a new chunk of virtual memory.
Definition virtual.cpp:113
void unload_range_from_process(const void *start_address, size_t size)
Unload a shared range of pages from this vmm.
Definition virtual.cpp:602
size_t memory_used()
Returns the amount of memory used.
Definition virtual.cpp:348
uint64_t * pml4_root_address_physical()
Get the physical address of the PML4 root.
Definition virtual.cpp:476
~VirtualMemoryManager()
Destroy the Virtual Memory Manager object and free all used pages.
Definition virtual.cpp:71
uint64_t * pml4_root_address()
Get the virtual address of the PML4 root.
Definition virtual.cpp:691
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.
Definition virtual.cpp:552
void * load_shared_memory(const string &name)
Load shared memory into the VMM's address space.
Definition virtual.cpp:487
Defines a PhysicalMemoryManager class for managing physical memory allocation and deallocation of pag...
constexpr uint64_t PAGE_SIZE
The size of a page (4KB)
Definition physical.h:92
A chunk of memory that has been freed and is available for allocation. A node in a linked list.
Definition virtual.h:71
size_t size
The size of the chunk's memory region in bytes.
Definition virtual.h:74
uintptr_t start_address
The first virtual address that this chunk covers.
Definition virtual.h:73
struct FreeChunk * next
Pointer to the next free chunk in the list (not sequential in memory)
Definition virtual.h:75
A container for a region of virtual memory that has been allocated by the Virtual Memory Manager.
Definition virtual.h:54
size_t flags
The flags for the memory (see VirtualFlags)
Definition virtual.h:58
uintptr_t start_address
The first virtual address that this chunk covers.
Definition virtual.h:56
size_t size
The size of the chunk's memory region in bytes.
Definition virtual.h:57
A region of virtual memory containing multiple chunks (should fit in one page).
Definition virtual.h:91
struct VirtualMemoryRegion * next
Pointer to the next region in the list.
Definition virtual.h:94
struct PACKED MaxOS::memory::VirtualMemoryRegion virtual_memory_region_t
Alias for VirtualMemoryRegion struct.
VirtualFlags
Flags for chunks used in virtual memory allocation.
Definition virtual.h:38
@ RESERVE
Reserve the memory but do not map any physical memory to it.
Definition virtual.h:40
constexpr size_t CHUNKS_PER_PAGE
Number of chunks per virtual memory region (limited to fit in one page)
Definition virtual.h:79
constexpr size_t VMM_RESERVED
The starting address for the VMM to start allocating from when in higher half (otherwise can cause co...
Definition virtual.h:80