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#include <memory/physical.h>
15#include <common/string.h>
16
17
18namespace MaxOS {
19 namespace memory {
20
31 typedef enum VirtualFlags {
32
33 RESERVE = (1 << 9),
34 SHARED = (1 << 10),
35
36 } virtual_flags_t;
37
47 typedef struct VirtualMemoryChunk {
48
49 uintptr_t start_address;
50 size_t size;
51 size_t flags;
52
53 } virtual_memory_chunk_t;
54
62 typedef struct FreeChunk {
63
64 uintptr_t start_address;
65 size_t size;
66 struct FreeChunk* next;
67
68 } free_chunk_t;
69
70 static const size_t CHUNKS_PER_PAGE = PAGE_SIZE / sizeof(virtual_memory_chunk_t) -
71 1;
72 static const size_t VMM_RESERVED = 0x138000000;
73
83 typedef struct PACKED VirtualMemoryRegion {
84
85 virtual_memory_chunk_t chunks[CHUNKS_PER_PAGE];
87
88 } virtual_memory_region_t;
89
97
98 private:
99 uint64_t* m_pml4_root_address;
100 uint64_t* m_pml4_root_physical_address;
101
102 virtual_memory_region_t* m_first_region;
103 virtual_memory_region_t* m_current_region;
104 size_t m_current_chunk = 0;
105 size_t m_next_available_address;
106
107 free_chunk_t* m_free_chunks = nullptr;
108 void add_free_chunk(uintptr_t start_address, size_t size);
109 free_chunk_t* find_and_remove_free_chunk(size_t size);
110
111 void new_region();
112 void fill_up_to_address(uintptr_t address, size_t flags, bool mark_used);
113
114 public:
117
118 void* allocate(size_t size, size_t flags);
119 void* allocate(uint64_t address, size_t size, size_t flags);
120 void free(void* address);
121
122 void* load_physical_into_address_space(uintptr_t physical_address, size_t size, size_t flags);
123
124 void* load_shared_memory(const string &name);
125 void* load_shared_memory(uintptr_t physical_address, size_t size);
126
127 uint64_t* pml4_root_address();
128 uint64_t* pml4_root_address_physical();
129
130 size_t memory_used();
131
132 };
133 }
134}
135
136
137#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:96
VirtualMemoryManager()
Construct a new Virtual Memory Manager object and set up the initial page tables (kernel mapped into ...
Definition virtual.cpp:22
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:455
void * allocate(size_t size, size_t flags)
Allocate a new chunk of virtual memory.
Definition virtual.cpp:119
size_t memory_used()
Returns the amount of memory used.
Definition virtual.cpp:320
uint64_t * pml4_root_address_physical()
Get the physical address of the PML4 root.
Definition virtual.cpp:407
~VirtualMemoryManager()
Destroy the Virtual Memory Manager object and free all used pages.
Definition virtual.cpp:81
uint64_t * pml4_root_address()
Get the virtual address of the PML4 root.
Definition virtual.cpp:521
void free(void *address)
Free a chunk of virtual memory.
Definition virtual.cpp:253
void * load_shared_memory(const string &name)
Load shared memory into the VMM's address space.
Definition virtual.cpp:418
Defines a PhysicalMemoryManager class for managing physical memory allocation and deallocation of pag...
Defines a String class for dynamically sized strings with various operations.
A chunk of memory that has been freed and is available for allocation. A node in a linked list.
Definition virtual.h:62
size_t size
*copydoc VirtualMemoryChunk::size
Definition virtual.h:65
uintptr_t start_address
*copydoc VirtualMemoryChunk::start_address
Definition virtual.h:64
struct FreeChunk * next
Pointer to the next free chunk in the list (not sequential in memory)
Definition virtual.h:66
A container for a region of virtual memory that has been allocated by the Virtual Memory Manager.
Definition virtual.h:47
size_t flags
The flags for the memory (see VirtualFlags)
Definition virtual.h:51
uintptr_t start_address
The first virtual address that this chunk covers.
Definition virtual.h:49
size_t size
The size of the chunk's memory region in bytes.
Definition virtual.h:50
A region of virtual memory containing multiple chunks (should fit in one page).
Definition virtual.h:83
struct VirtualMemoryRegion * next
Pointer to the next region in the list.
Definition virtual.h:86
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:31
@ RESERVE
Reserve the memory but do not map any physical memory to it.
Definition virtual.h:33