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#include <aurora.h>
44
45#ifdef ARCH_X64
46#define X86_64_PAGING_PRESENT 0x1
47#define X86_64_PAGING_WRITABLE 0x2
48#define X86_64_PAGING_USER 0x4
49#define X86_64_PAGING_NO_EXECUTE 0x80000
50#define X86_64_PAGING_NO_CACHING 0x200000
51#define X86_64_PAGING_WRITE_THROUGH 0x400000
52#else
53#define PTE_VALID (1ULL << 0)
54#define PTE_TABLE (1ULL << 1)
55#define PTE_BLOCK (0ULL << 1)
56#define PTE_AF (1ULL << 10)
57#define PTE_SH_INNER (3ULL << 8)
58#define PTE_AP_RW (0ULL << 6) //RW access
59#define PTE_AP_RW_USER (1ULL << 6) //1ULL << 6
60#define PTE_ATTR_IDX_0 (0ULL << 2) //Device memory
61#define PTE_ATTR_IDX_1 (1ULL << 2) //Normal memory
62#define PTE_USER_EXECUTABLE (0ULL << 54)
63#define PTE_KERNEL_EXECUTABLE (0ULL << 53)
64#define PTE_USER_NOT_EXECUTABLE (1ULL << 54)
65#define PTE_KERNEL_NOT_EXECUTABLE (1ULL << 53)
66
67#define PTE_NORMAL_MEM PTE_ATTR_IDX_1
68#define PTE_DEVICE_MEM PTE_ATTR_IDX_0
69#define PTE_NORMAL_NON_CACHEABLE (2ULL << 2)
70#endif
71
72#define PHYSICAL_MEM_BASE 0xFFFF800000000000
73#define MMIO_BASE 0xFFFFFF1000000000
74
75#define PAGE_SHIFT 12
76
77#define VIRT_GETPAGE_CREATE (1<<0)
78#define VIRT_GETPAGE_ONLY_RET (1<<1)
79
80#define KERNEL_BASE_ADDRESS 0xFFFFE00000000000
81#define USER_BASE_ADDRESS 0x0000000060000000 //0x0000400000000000
82#define USER_END_ADDRESS 0x0000000080000000
83
84#ifdef ARCH_X64
85#define PAGE_SIZE 4096
86#else
87#define PAGE_SIZE (1ULL << PAGE_SHIFT) //4096
88#define PAGE_MASK (PAGE_SIZE - 1)
89#endif
90
91/* aligns a virtual address to its immediate page boundary */
92#define VIRT_ADDR_ALIGN(vaddr) (vaddr & ~(PAGE_SIZE - 1))
93
94#define PAGE_ALIGN(value) (((PAGE_SIZE-1)&value) ? ((value + PAGE_SIZE) & ~(PAGE_SIZE-1)) : value)
95
96#ifdef ARCH_X64
97#pragma pack(push,1)
98/*
99 * AuVPage -- structure of virtual page
100 */
101typedef union _AuVPage_ {
102 struct {
103 uint64_t present : 1;
104 uint64_t writable : 1;
105 uint64_t user : 1;
106 uint64_t write_through : 1;
107 uint64_t cache_disable : 1;
108 uint64_t access : 1;
109 uint64_t dirty : 1;
110 uint64_t size : 1;
111 uint64_t global : 1;
112 uint64_t cow : 1;
113 uint64_t _avail : 2;
114 uint64_t page : 28;
115 uint64_t reserved : 12;
116 uint64_t nx : 1;
117 }bits;
118 uint64_t raw;
119}AuVPage;
120#pragma pack(pop)
121#elif ARCH_ARM64
122
123#pragma pack(push,1)
124typedef union _AuVPage_ {
125 struct {
126 uint64_t present : 1;
127 uint64_t table_page : 1;
128 uint64_t attrindex : 3;
129 uint64_t ns : 1;
130 uint64_t ap : 2;
131 uint64_t sh : 2;
132 uint64_t af : 1;
133 uint64_t ng : 1;
134 uint64_t page : 36;
135 uint64_t reserved : 4;
136 uint64_t contiguous : 1;
137 uint64_t pxn : 1;
138 uint64_t uxn : 1;
139 uint64_t avail : 4;
140 uint64_t ignored : 5;
141 }bits;
142
143 struct {
144 uint64_t valid : 1;
145 uint64_t table : 1;
146 uint64_t next : 46;
147 uint64_t reserved : 4;
148 uint64_t ignored : 7;
149 uint64_t pxntable : 1;
150 uint64_t xntable : 1;
151 uint64_t aptable : 2;
152 uint64_t nstable : 1;
153 }tableBits;
154 uint64_t raw;
155}AuVPage;
156#pragma pack(pop)
157#endif
158
162extern void AuVmmngrInitialize();
163
175AU_EXTERN AU_EXPORT AuVPage * AuVmmngrGetPage(uint64_t virt_addr, uint8_t flags, uint8_t mode);
176
184AU_EXTERN AU_EXPORT bool AuMapPage(uint64_t phys_addr, uint64_t virt_addr, uint8_t attrib);
185
195AU_EXTERN AU_EXPORT bool AuMapPageEx(uint64_t *pml4i, uint64_t phys_addr, uint64_t virt_addr, uint8_t attrib);
196
203AU_EXTERN AU_EXPORT void* AuMapMMIO(uint64_t phys_addr, size_t page_count);
204
213AU_EXTERN AU_EXPORT uint64_t* AuGetFreePage(bool user, void* ptr);
214
221AU_EXTERN AU_EXPORT void AuFreePages(uint64_t virt_addr, bool free_physical, size_t s);
222
229
237
238/*
239* AuGetPhysicalAddressEx -- translates logical address
240* to its physical address
241* @param cr3 -- Page level governor
242* @param virt_addr -- virtual address
243*/
245
251
252/*
253* AuGetRootPageTable -- returns the root
254* page table
255*/
257
262extern void AuVmmngrBootFree();
263
269extern void AuVmmngrCloneAddressSpace(uint64_t *destcr3, uint64_t* srccr3);
270
271#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:497
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)
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:50
#define AU_EXPORT
Definition aurora.h:38
unsigned char uint8_t
Definition acefiex.h:161
COMPILER_DEPENDENT_UINT64 uint64_t
Definition acefiex.h:165
bool dirty
Definition term.cpp:58