kernel
Loading...
Searching...
No Matches
mminit.c
Go to the documentation of this file.
1/*++
2
3Module Name:
4
5 mminit.c
6
7Purpose:
8
9 This translation unit contains the implementation of memory manager initialization routines.
10
11Author:
12
13 slep (Matanel) 2025.
14
15Revision History:
16
17--*/
18
19#include "../../includes/mm.h"
20#include "../../includes/me.h"
21#include "../../includes/mg.h"
22#include "../../includes/ob.h"
23#include "../../assert.h"
24
25#define IA32_PAT 0x277
26
28
29static
30bool
31MiIsPATAvailable(void)
32
33{
34 uint32_t eax, ebx, ecx, edx;
35 __cpuid(1, eax, ebx, ecx, edx);
36 return (edx & (1 << 16)) != 0;
37}
38
39static
40void
41MiInitializePAT(void)
42
43{
44 uint64_t pat =
45 0x00 | // 0 = WB
46 (0x01ULL << 8) | // 1 = WT
47 (0x02ULL << 16) | // 2 = UC-
48 (0x03ULL << 24) | // 3 = UC
49 (0x00ULL << 32) | // 4 = WB
50 (0x01ULL << 40) | // 5 = WC
51 (0x02ULL << 48) | // 6 = UC-
52 (0x03ULL << 56); // 7 = UC
53
54 __writemsr(IA32_PAT, pat);
55}
56
59 void
60)
61
62{
63 OBJECT_TYPE_INITIALIZER ObjectTypeInitializer;
64 kmemset(&ObjectTypeInitializer, 0, sizeof(OBJECT_TYPE_INITIALIZER));
65
66 // Initiate types.
67 ObjectTypeInitializer.PoolType = NonPagedPool;
68 ObjectTypeInitializer.DeleteProcedure = MmpDeleteSection;
69 ObjectTypeInitializer.ValidAccessRights = MT_SECTION_ALL_ACCESS;
70 ObjectTypeInitializer.DumpProcedure = NULL; // TODO DUMP PROC!
71
72 MTSTATUS Status = ObCreateObjectType("Section", &ObjectTypeInitializer, &MmSectionType);
73 return Status;
74}
75
76bool
78 IN uint8_t Phase,
79 IN PBOOT_INFO BootInformation
80)
81
82/*++
83
84 Routine description:
85
86 Initializes the memory manager of the system.
87
88 Arguments:
89
90 [IN] uint8_t Phase - Specifies the phase of the system for correct init routines. (enum SYSTEM_PHASE_ROUTINE)
91 [IN] PBOOT_INFO BootInformation - The boot information supplied by the UEFI Bootloader (can be NULL in later phases, look below)
92
93 Phase Demands:
94
95 1 - BootInformation
96 2 - None.
97
98 Phase Does:
99
100 1 (SYSTEM_PHASE_INITIALIZE_ALL) - Initializes PAT and the core memory managment routines. (PFN Database, Virtual Address bitmap, PAT, PTE Database, etc.)
101
102 2 (SYSTEM_PHASE_INITIALIZE_PAT_ONLY) - Initializes PAT only (used in AP startup)
103
104 Return Values:
105
106 True or false if the phase given has succeeded initilization.
107
108--*/
109
110{
111 // Currently we only support the first and only phase.
112 if (Phase == SYSTEM_PHASE_INITIALIZE_ALL) {
113
114 // Initialize PAT (Page Attribute Table)
115 bool PatAvailable = MiIsPATAvailable();
116 assert(PatAvailable == true);
117 if (PatAvailable) {
118 MiInitializePAT();
119 }
120
121 // Initialize all memory managment routines (PFN Database, VA Space, Pools, MMIO, PTE Database)
122 // If we fail init of one of them, we bugcheck, since they are mandatory for operation.
123 MTSTATUS st = MiInitializePfnDatabase(BootInformation);
124 if (MT_FAILURE(st)) {
127 (void*)(uintptr_t)st,
128 NULL,
129 NULL,
130 NULL
131 );
132 }
133
136 }
137
139 if (MT_FAILURE(st)) {
142 (void*)(uintptr_t)st,
143 NULL,
144 NULL,
145 NULL
146 );
147 }
148
149 // Phase 1 Done.
150 return true;
151 }
152
153 else if (Phase == SYSTEM_PHASE_INITIALIZE_PAT_ONLY) {
154 // Phase only initializes PAT for the current core.
155 // Initialize PAT (Page Attribute Table)
156 bool PatAvailable = MiIsPATAvailable();
157 assert(PatAvailable == true);
158 if (PatAvailable) {
159 MiInitializePAT();
160 }
161
162 // Return if PAT is available on the current core or not (if available, it's initialized)
163 return PatAvailable;
164 }
165
166 else {
167 // Only phase 1 & 2 are supported currently.
169 }
170}
171
172extern GOP_PARAMS gop_local;
174
175void
177 IN PBOOT_INFO BootInfo
178)
179
180
181/*++
182
183 Routine description:
184
185 Moves UEFI Memory that is mapped below the kernel half to the kernel half.
186
187 Arguments:
188
189 [IN] PBOOT_INFO BootInformation - The boot information supplied by the UEFI Bootloader.
190
191
192 Return Values:
193
194 None.
195
196 Notes:
197
198 Currently the only UEFI memory that is below the higher half is the GOP. (and RSDP, but that is processed during kernel startup, so its fine)
199
200--*/
201
202{
203 // Move GOP to higher half, luckily i developed this extremely advanced memory api (its just advancing pointers)
204 uintptr_t Phys = MiTranslateVirtualToPhysical((void*)gop_local.FrameBufferBase);
205
206#ifdef DEBUG
207 uint64_t oldBase = gop_local.FrameBufferBase;
208#endif
209
210 gop_local.FrameBufferBase = (uint64_t)MmMapIoSpace(Phys, gop_local.FrameBufferSize, MmCached);
211 assert(gop_local.FrameBufferBase != Phys);
212 assert((void*)gop_local.FrameBufferBase != NULL);
213
214 // Unmap the previous PTE. (was a 1:1 identity map, so thats why we use the phys addr)
216
217#ifdef DEBUG
218 assert(MmIsAddressValid((uintptr_t)oldBase) == false);
219#endif
220 uintptr_t BootInfoPhys = MiTranslateVirtualToPhysical((void*)BootInfo);
221
222 // Destroy the boot_info struct PTE.
223 // First, memory set it to 0 (just for good measure)
224 kmemset(BootInfo, 0, sizeof(BOOT_INFO));
225
226 // Great, unmap it now.
227 // NOTE: Is this safe? This unmaps the whole 4KiB page of the BootInfo, so this assumes theres no data near it (that we need)
228 assert(sizeof(BOOT_INFO) <= VirtualPageSize); // If the size is larger, we would need to do a for loop.
229 MiUnmapPte(MiGetPtePointer((uintptr_t)BootInfo));
230 assert(MmIsAddressValid((uintptr_t)BootInfo) == false);
231
232 // Free its physical frame.
234}
BOOT_INFO boot_info_local
Definition kernel.c:27
#define IN
Definition annotations.h:8
#define assert(...)
Definition assert.h:57
NORETURN void MeBugCheck(IN enum _BUGCHECK_CODES BugCheckCode)
Definition bugcheck.c:220
NORETURN void MeBugCheckEx(IN enum _BUGCHECK_CODES BugCheckCode, IN void *BugCheckParameter1, IN void *BugCheckParameter2, IN void *BugCheckParameter3, IN void *BugCheckParameter4)
Definition bugcheck.c:245
GOP_PARAMS gop_local
Definition gop.c:223
struct _BOOT_INFO * PBOOT_INFO
struct _GOP_PARAMS GOP_PARAMS
struct _BOOT_INFO BOOT_INFO
FORCEINLINE void __writemsr(uint32_t msr, uint64_t value)
Definition intrin.h:215
uintptr_t MiTranslateVirtualToPhysical(IN void *VirtualAddress)
Definition map.c:512
PMMPTE MiGetPtePointer(IN uintptr_t va)
Definition map.c:76
void MiUnmapPte(IN PMMPTE pte)
Definition map.c:385
@ PFN_DATABASE_INIT_FAILURE
Definition me.h:119
@ POOL_INIT_FAILURE
Definition me.h:121
@ INVALID_INITIALIZATION_PHASE
Definition me.h:124
@ VA_SPACE_INIT_FAILURE
Definition me.h:120
#define PHYSICAL_TO_PPFN(PHYS)
Definition mm.h:64
@ SYSTEM_PHASE_INITIALIZE_PAT_ONLY
Definition mm.h:414
@ SYSTEM_PHASE_INITIALIZE_ALL
Definition mm.h:413
@ NonPagedPool
Definition mm.h:355
@ MmCached
Definition mm.h:382
#define PPFN_TO_INDEX(PPFN)
Definition mm.h:135
FORCEINLINE void * kmemset(void *dest, int64_t val, uint64_t len)
Definition mm.h:655
#define MmIsAddressValid(VirtualAddress)
Definition mm.h:258
#define MT_SECTION_ALL_ACCESS
Definition mm.h:270
#define VirtualPageSize
Definition mm.h:53
#define IA32_PAT
Definition mminit.c:25
POBJECT_TYPE MmSectionType
Definition mminit.c:27
void MiMoveUefiDataToHigherHalf(IN PBOOT_INFO BootInfo)
Definition mminit.c:176
bool MmInitSystem(IN uint8_t Phase, IN PBOOT_INFO BootInformation)
Definition mminit.c:77
MTSTATUS MmInitSections(void)
Definition mminit.c:58
void * MmMapIoSpace(IN uintptr_t PhysicalAddress, IN size_t NumberOfBytes, IN MEMORY_CACHING_TYPE CacheType)
Definition mmio.c:269
#define MT_FAILURE(Status)
Definition mtstatus.h:16
int32_t MTSTATUS
Definition mtstatus.h:12
MTSTATUS ObCreateObjectType(IN char *TypeName, IN POBJECT_TYPE_INITIALIZER ObjectTypeInitializer, OUT POBJECT_TYPE *ReturnedObjectType)
Definition ob.c:62
struct _OBJECT_TYPE * POBJECT_TYPE
struct _OBJECT_TYPE_INITIALIZER OBJECT_TYPE_INITIALIZER
NOINLINE void MiReleasePhysicalPage(IN PAGE_INDEX PfnIndex)
Definition pfn.c:441
MTSTATUS MiInitializePfnDatabase(IN PBOOT_INFO BootInfo)
Definition pfn.c:102
MTSTATUS MiInitializePoolSystem(void)
Definition pool.c:36
void MmpDeleteSection(void *Object)
Definition section.c:198
OB_DELETE_METHOD DeleteProcedure
Definition ob.h:43
OB_DUMP_METHOD DumpProcedure
Definition ob.h:42
POOL_TYPE PoolType
Definition ob.h:39
uint32_t ValidAccessRights
Definition ob.h:41
bool MiInitializePoolVaSpace(void)
Definition va.c:35