XenevaOS
Loading...
Searching...
No Matches
vmmngr.h
Go to the documentation of this file.
1
32#ifndef __VMMNGR_H__
33#define __VMMNGR_H__
34
35/*
36 * VMMNGR -- Virtual Memory Manager is the
37 * base of memory management, this module
38 * will contain memory management code for
39 * every platform
40 */
41
42#include <stdint.h>
43#if defined(__GNUC__) || defined(__clang__)
44#ifndef __cplusplus
45#include <stdbool.h>
46#endif
47#endif
48#include <aurora.h>
49
50#ifdef ARCH_X64
51#define X86_64_PAGING_PRESENT 0x1
52#define X86_64_PAGING_WRITABLE 0x2
53#define X86_64_PAGING_USER 0x4
54#define X86_64_PAGING_NO_EXECUTE 0x80000
55#define X86_64_PAGING_NO_CACHING 0x200000
56#define X86_64_PAGING_WRITE_THROUGH 0x400000
57#else
58#define PTE_VALID (1ULL << 0)
59#define PTE_TABLE (1ULL << 1)
60#define PTE_BLOCK (0ULL << 1)
61#define PTE_AF (1ULL << 10)
62#define PTE_SH_INNER (3ULL << 8)
63#define PTE_AP_RW (0ULL << 6) //RW access
64#define PTE_AP_RW_USER (1ULL << 6) //1ULL << 6
65#define PTE_ATTR_IDX_0 (0ULL << 2) //Device memory
66#define PTE_ATTR_IDX_1 (1ULL << 2) //Normal memory
67#define PTE_USER_EXECUTABLE (0ULL << 54)
68#define PTE_KERNEL_EXECUTABLE (0ULL << 53)
69#define PTE_USER_NOT_EXECUTABLE (1ULL << 54)
70#define PTE_KERNEL_NOT_EXECUTABLE (1ULL << 53)
71
72#define PTE_NORMAL_MEM PTE_ATTR_IDX_1
73#define PTE_DEVICE_MEM PTE_ATTR_IDX_0
74#define PTE_NORMAL_NON_CACHEABLE (2ULL << 2)
75#endif
76
77#define PHYSICAL_MEM_BASE 0xFFFF800000000000
78#define MMIO_BASE 0xFFFFFF1000000000
79
80#define PAGE_SHIFT 12
81
82#define VIRT_GETPAGE_CREATE (1<<0)
83#define VIRT_GETPAGE_ONLY_RET (1<<1)
84
85#define KERNEL_BASE_ADDRESS 0xFFFFE00000000000
86#define USER_BASE_ADDRESS 0x0000000060000000 //0x0000400000000000
87#define USER_END_ADDRESS 0x0000000080000000
88
89#ifdef ARCH_X64
90#define PAGE_SIZE 4096
91#else
92#define PAGE_SIZE (1ULL << PAGE_SHIFT) //4096
93#define PAGE_MASK (PAGE_SIZE - 1)
94#endif
95
96/* aligns a virtual address to its immediate page boundary */
97#define VIRT_ADDR_ALIGN(vaddr) (vaddr & ~(PAGE_SIZE - 1))
98
99#define PAGE_ALIGN(value) (((PAGE_SIZE-1)&value) ? ((value + PAGE_SIZE) & ~(PAGE_SIZE-1)) : value)
100
101#ifdef ARCH_X64
102#pragma pack(push,1)
103/*
104 * AuVPage -- structure of virtual page
105 */
106typedef union _AuVPage_ {
107 struct {
108 uint64_t present : 1;
109 uint64_t writable : 1;
110 uint64_t user : 1;
111 uint64_t write_through : 1;
112 uint64_t cache_disable : 1;
113 uint64_t access : 1;
114 uint64_t dirty : 1;
115 uint64_t size : 1;
116 uint64_t global : 1;
117 uint64_t cow : 1;
118 uint64_t _avail : 2;
119 uint64_t page : 28;
120 uint64_t reserved : 12;
121 uint64_t nx : 1;
122 }bits;
123 uint64_t raw;
124}AuVPage;
125#pragma pack(pop)
126#elif ARCH_ARM64
127
128#pragma pack(push,1)
129typedef union _AuVPage_ {
130 struct {
131 uint64_t present : 1;
132 uint64_t table_page : 1;
133 uint64_t attrindex : 3;
134 uint64_t ns : 1;
135 uint64_t ap : 2;
136 uint64_t sh : 2;
137 uint64_t af : 1;
138 uint64_t ng : 1;
139 uint64_t page : 36;
140 uint64_t reserved : 4;
141 uint64_t contiguous : 1;
142 uint64_t pxn : 1;
143 uint64_t uxn : 1;
144
145 /* software flags */
146 uint64_t cow : 1;
147 uint64_t file_backed : 1;
148 uint64_t avail : 2;
149
150 uint64_t ignored : 5;
151 }bits;
152
153 struct {
154 uint64_t valid : 1;
155 uint64_t table : 1;
156 uint64_t next : 46;
157 uint64_t reserved : 4;
158 uint64_t ignored : 7;
159 uint64_t pxntable : 1;
160 uint64_t xntable : 1;
161 uint64_t aptable : 2;
162 uint64_t nstable : 1;
163 }tableBits;
164 uint64_t raw;
165}AuVPage;
166#pragma pack(pop)
167#endif
168
172extern void AuVmmngrInitialize();
173
185AU_EXTERN AU_EXPORT AuVPage * AuVmmngrGetPage(uint64_t virt_addr, uint8_t flags, uint8_t mode);
186
194AU_EXTERN AU_EXPORT bool AuMapPage(uint64_t phys_addr, uint64_t virt_addr, uint8_t attrib);
195
205AU_EXTERN AU_EXPORT bool AuMapPageEx(uint64_t *pml4i, uint64_t phys_addr, uint64_t virt_addr, uint8_t attrib);
206
213AU_EXTERN AU_EXPORT void* AuMapMMIO(uint64_t phys_addr, size_t page_count);
214
223AU_EXTERN AU_EXPORT uint64_t* AuGetFreePage(bool user, void* ptr);
224
231AU_EXTERN AU_EXPORT void AuFreePages(uint64_t virt_addr, bool free_physical, size_t s);
232
239
247
248/*
249* AuGetPhysicalAddressEx -- translates logical address
250* to its physical address
251* @param cr3 -- Page level governor
252* @param virt_addr -- virtual address
253*/
255
261
262/*
263* AuGetRootPageTable -- returns the root
264* page table
265*/
267
272extern void AuVmmngrBootFree();
273
279extern void AuVmmngrCloneAddressSpace(uint64_t *destcr3, uint64_t* srccr3);
280
281#endif
AU_EXTERN AU_EXPORT AuVPage * AuVmmngrGetPage(uint64_t virt_addr, uint8_t flags, uint8_t mode)
Definition vmmngr.cpp:125
AU_EXTERN AU_EXPORT void * AuGetPhysicalAddress(uint64_t virt_addr)
AuGetPhysicalAddress – returns the physical address from a virtual address.
Definition vmmngr.cpp:418
AU_EXTERN AU_EXPORT void AuFreePages(uint64_t virt_addr, bool free_physical, size_t s)
AuFreePages – frees up contiguous pages.
Definition vmmngr.cpp:389
AU_EXTERN AU_EXPORT bool AuMapPage(uint64_t phys_addr, uint64_t virt_addr, uint8_t attrib)
AuMapPage – Maps a virtual page to physical frame.
Definition vmmngr.cpp:198
void AuVmmngrInitialize()
AuVmmngrInitialize – initialize the virtual memory manager.
Definition vmmngr.cpp:494
AU_EXTERN AU_EXPORT bool AuMapPageEx(uint64_t *pml4i, uint64_t phys_addr, uint64_t virt_addr, uint8_t attrib)
AuMapPageEx – Maps a virtual page to physical frame in given page level.
Definition vmmngr.cpp:263
void AuVmmngrBootFree()
Definition vmmngr.cpp:504
AU_EXTERN AU_EXPORT uint64_t * AuGetRootPageTable()
Definition vmmngr.cpp:484
AU_EXTERN AU_EXPORT void AuUpdatePageFlags(uint64_t virt_addr, uint64_t flags)
AuFreePages – frees up contiguous pages.
Definition vmmngr.c:519
AU_EXTERN AU_EXPORT void * AuMapMMIO(uint64_t phys_addr, size_t page_count)
AuMapMMIO – Maps Memory Mapped I/O addresses.
Definition vmmngr.cpp:323
AU_EXTERN AU_EXPORT uint64_t * AuGetFreePage(bool user, void *ptr)
AuGetFreePage – Checks for free page.
Definition vmmngr.cpp:341
AU_EXTERN AU_EXPORT void * AuGetPhysicalAddressEx(uint64_t *cr3, uint64_t virt_addr)
AuGetPhysicalAddressEx – returns the physical address from a virtual address.
Definition vmmngr.cpp:441
AU_EXTERN AU_EXPORT uint64_t * AuCreateVirtualAddressSpace()
AuCreateVirtualAddressSpace – create a new virtual address space.
Definition vmmngr.cpp:463
void AuVmmngrCloneAddressSpace(uint64_t *destcr3, uint64_t *srccr3)
AuVmmngrCloneAddressSpace – clones a given address space.
Definition vmmngr.cpp:518
#define AU_EXTERN
Definition aurora.h:55
#define AU_EXPORT
Definition aurora.h:41
unsigned char uint8_t
Definition acefiex.h:161
COMPILER_DEPENDENT_UINT64 uint64_t
Definition acefiex.h:165
bool dirty
Definition term.cpp:54