OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
VirtualMemory.h
Go to the documentation of this file.
1
18#ifndef VIRTUAL_MEMORY_H
19#define VIRTUAL_MEMORY_H
20
21#pragma pack(push, 1)
22
23//
24// Page-Map Level-4 Offset (PML4) and
25// Page-Directory-Pointer Offset (PDPE) entries 4K & 2MB
26//
27
28typedef union {
29 struct {
30 UINT64 Present : 1; // 0 = Not present in memory, 1 = Present in memory
31 UINT64 ReadWrite : 1; // 0 = Read-Only, 1= Read/Write
32 UINT64 UserSupervisor : 1; // 0 = Supervisor, 1=User
33 UINT64 WriteThrough : 1; // 0 = Write-Back caching, 1=Write-Through caching
34 UINT64 CacheDisabled : 1; // 0 = Cached, 1=Non-Cached
35 UINT64 Accessed : 1; // 0 = Not accessed, 1 = Accessed (set by CPU)
36 UINT64 Reserved : 1; // Reserved
37 UINT64 MustBeZero : 2; // Must Be Zero
38 UINT64 Available : 3; // Available for use by system software
39 UINT64 PageTableBaseAddress : 40; // Page Table Base Address
40 UINT64 AvabilableHigh : 11; // Available for use by system software
41 UINT64 Nx : 1; // No Execute bit
42 } Bits;
43 UINT64 Uint64;
45
46//
47// Page Table Entry 4KB
48//
49typedef union {
50 struct {
51 UINT64 Present : 1; // 0 = Not present in memory, 1 = Present in memory
52 UINT64 ReadWrite : 1; // 0 = Read-Only, 1= Read/Write
53 UINT64 UserSupervisor : 1; // 0 = Supervisor, 1=User
54 UINT64 WriteThrough : 1; // 0 = Write-Back caching, 1=Write-Through caching
55 UINT64 CacheDisabled : 1; // 0 = Cached, 1=Non-Cached
56 UINT64 Accessed : 1; // 0 = Not accessed, 1 = Accessed (set by CPU)
57 UINT64 Dirty : 1; // 0 = Not Dirty, 1 = written by processor on access to page
58 UINT64 PAT : 1; // Combines with CD, WT and MTRR to define true caching type
59 UINT64 Global : 1; // 0 = Not global page, 1 = global page TLB not cleared on CR3 write
60 UINT64 Available : 3; // Available for use by system software
61 UINT64 PageTableBaseAddress : 40; // Page Table Base Address
62 UINT64 AvabilableHigh : 11; // Available for use by system software
63 UINT64 Nx : 1; // 0 = Execute Code, 1 = No Code Execution
64 } Bits;
65 UINT64 Uint64;
67
68//
69// Page Table Entry 2MB
70//
71typedef union {
72 struct {
73 UINT64 Present : 1; // 0 = Not present in memory, 1 = Present in memory
74 UINT64 ReadWrite : 1; // 0 = Read-Only, 1= Read/Write
75 UINT64 UserSupervisor : 1; // 0 = Supervisor, 1=User
76 UINT64 WriteThrough : 1; // 0 = Write-Back caching, 1=Write-Through caching
77 UINT64 CacheDisabled : 1; // 0 = Cached, 1=Non-Cached
78 UINT64 Accessed : 1; // 0 = Not accessed, 1 = Accessed (set by CPU)
79 UINT64 Dirty : 1; // 0 = Not Dirty, 1 = written by processor on access to page
80 UINT64 MustBe1 : 1; // Must be 1
81 UINT64 Global : 1; // 0 = Not global page, 1 = global page TLB not cleared on CR3 write
82 UINT64 Available : 3; // Available for use by system software
83 UINT64 PAT : 1; // Combines with CD, WT and MTRR to define true caching type
84 UINT64 MustBeZero : 8; // Must be zero;
85 UINT64 PageTableBaseAddress : 31; // Page Table Base Address
86 UINT64 AvabilableHigh : 11; // Available for use by system software
87 UINT64 Nx : 1; // 0 = Execute Code, 1 = No Code Execution
88 } Bits;
89 UINT64 Uint64;
91
92//
93// Page Table Entry 1GB
94//
95typedef union {
96 struct {
97 UINT64 Present : 1; // 0 = Not present in memory, 1 = Present in memory
98 UINT64 ReadWrite : 1; // 0 = Read-Only, 1= Read/Write
99 UINT64 UserSupervisor : 1; // 0 = Supervisor, 1=User
100 UINT64 WriteThrough : 1; // 0 = Write-Back caching, 1=Write-Through caching
101 UINT64 CacheDisabled : 1; // 0 = Cached, 1=Non-Cached
102 UINT64 Accessed : 1; // 0 = Not accessed, 1 = Accessed (set by CPU)
103 UINT64 Dirty : 1; // 0 = Not Dirty, 1 = written by processor on access to page
104 UINT64 MustBe1 : 1; // Must be 1
105 UINT64 Global : 1; // 0 = Not global page, 1 = global page TLB not cleared on CR3 write
106 UINT64 Available : 3; // Available for use by system software
107 UINT64 PAT : 1; // Combines with CD, WT and MTRR to define true caching type
108 UINT64 MustBeZero : 17; // Must be zero;
109 UINT64 PageTableBaseAddress : 22; // Page Table Base Address
110 UINT64 AvabilableHigh : 11; // Available for use by system software
111 UINT64 Nx : 1; // 0 = Execute Code, 1 = No Code Execution
112 } Bits;
113 UINT64 Uint64;
115
116//
117// PAT index bits.
118//
119typedef union {
120 struct {
121 UINT8 WriteThrough : 1; // 0 = Write-Back caching, 1=Write-Through caching
122 UINT8 CacheDisabled : 1; // 0 = Cached, 1=Non-Cached
123 UINT8 PAT : 1; // Combines with CD, WT and MTRR to define true caching type
124 UINT8 Reserved : 5; // Reserved
125 } Bits;
126 UINT8 Index;
127} PAT_INDEX;
128
129typedef union {
130 struct {
131 UINT64 PhysPgOffset : 12; // 0 = Physical Page Offset
132 UINT64 PTOffset : 9; // 0 = Page Table Offset
133 UINT64 PDOffset : 9; // 0 = Page Directory Offset
134 UINT64 PDPOffset : 9; // 0 = Page Directory Pointer Offset
135 UINT64 PML4Offset : 9; // 0 = Page Map Level 4 Offset
136 UINT64 SignExtend : 16; // 0 = Sign Extend
137 } Pg4K;
138 struct {
139 UINT64 PhysPgOffset : 21; // 0 = Physical Page Offset
140 UINT64 PDOffset : 9; // 0 = Page Directory Offset
141 UINT64 PDPOffset : 9; // 0 = Page Directory Pointer Offset
142 UINT64 PML4Offset : 9; // 0 = Page Map Level 4 Offset
143 UINT64 SignExtend : 16; // 0 = Sign Extend
144 } Pg2M;
145 struct {
146 UINT64 PhysPgOffset : 30; // 0 = Physical Page Offset
147 UINT64 PDPOffset : 9; // 0 = Page Directory Pointer Offset
148 UINT64 PML4Offset : 9; // 0 = Page Map Level 4 Offset
149 UINT64 SignExtend : 16; // 0 = Sign Extend
150 } Pg1G;
151 UINT64 Uint64;
153
154#define VA_FIX_SIGN_EXTEND(VA) ((VA).Pg4K.SignExtend = ((VA).Pg4K.PML4Offset & 0x100U) ? 0xFFFFU : 0U);
155
156#pragma pack(pop)
157
158#define CR0_WP BIT16
159
160#define CR3_ADDR_MASK 0x000FFFFFFFFFF000ull
161#define CR3_FLAG_PWT 0x0000000000000008ull
162#define CR3_FLAG_PCD 0x0000000000000010ull
163
164#define PAGING_4K_ADDRESS_MASK_64 0x000FFFFFFFFFF000ull
165#define PAGING_2M_ADDRESS_MASK_64 0x000FFFFFFFE00000ull
166#define PAGING_1G_ADDRESS_MASK_64 0x000FFFFFC0000000ull
167
168#endif // VIRTUAL_MEMORY_H
UINT8 WriteThrough
UINT8 CacheDisabled
UINT64 PhysPgOffset