7c3dcdfe54091d58c975db22c1b1ecf873cb0cc7
[reactos.git] / reactos / boot / environ / lib / mm / mm.c
1 /*
2 * COPYRIGHT: See COPYING.ARM in the top level directory
3 * PROJECT: ReactOS UEFI Boot Library
4 * FILE: boot/environ/lib/mm/mm.c
5 * PURPOSE: Boot Library Memory Manager Core
6 * PROGRAMMER: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include "bl.h"
12
13 /* DATA VARIABLES ************************************************************/
14
15 BL_TRANSLATION_TYPE MmTranslationType, MmOriginalTranslationType;
16 ULONG MmDescriptorCallTreeCount;
17
18 /* FUNCTIONS *****************************************************************/
19
20 NTSTATUS
21 MmTrInitialize (
22 VOID
23 )
24 {
25 /* Nothing to track if we're using physical memory */
26 if (MmTranslationType == BlNone)
27 {
28 return STATUS_SUCCESS;
29 }
30
31 /* TODO */
32 EarlyPrint(L"Required for protected mode\n");
33 return STATUS_NOT_IMPLEMENTED;
34 }
35
36 NTSTATUS
37 BlMmRemoveBadMemory (
38 VOID
39 )
40 {
41 /* FIXME: Read BCD option to see what bad memory to remove */
42 return STATUS_SUCCESS;
43 }
44
45 NTSTATUS
46 BlpMmInitialize (
47 _In_ PBL_MEMORY_DATA MemoryData,
48 _In_ BL_TRANSLATION_TYPE TranslationType,
49 _In_ PBL_LIBRARY_PARAMETERS LibraryParameters
50 )
51 {
52 NTSTATUS Status;
53
54 /* Take a reference */
55 MmDescriptorCallTreeCount = 1;
56
57 /* Only support valid translation types */
58 if ((TranslationType > BlPae) || (LibraryParameters->TranslationType > BlPae))
59 {
60 /* Bail out */
61 EarlyPrint(L"Invalid translation types present\n");
62 Status = STATUS_INVALID_PARAMETER;
63 goto Quickie;
64 }
65
66 /* Initialize memory descriptors */
67 MmMdInitialize(0, LibraryParameters);
68
69 /* Remember the page type we came in with */
70 MmOriginalTranslationType = TranslationType;
71
72 /* Initialize the physical page allocator */
73 Status = MmPaInitialize(MemoryData,
74 LibraryParameters->MinimumAllocationCount);
75 if (!NT_SUCCESS(Status))
76 {
77 goto Quickie;
78 }
79
80 /* Initialize the memory tracker */
81 Status = MmTrInitialize();
82 if (!NT_SUCCESS(Status))
83 {
84 //MmArchDestroy();
85 //MmPaDestroy(1);
86 goto Quickie;
87 }
88
89 /* Initialize paging, large pages, self-mapping, PAE, if needed */
90 Status = MmArchInitialize(1,
91 MemoryData,
92 TranslationType,
93 LibraryParameters->TranslationType);
94 if (NT_SUCCESS(Status))
95 {
96 /* Save the newly active transation type */
97 MmTranslationType = LibraryParameters->TranslationType;
98
99 /* Initialize the heap allocator now */
100 Status = MmHaInitialize(LibraryParameters->MinimumHeapSize,
101 LibraryParameters->HeapAllocationAttributes);
102 }
103
104 /* If Phase 1 init failed, bail out */
105 if (!NT_SUCCESS(Status))
106 {
107 /* Kill everything set setup so far */
108 //MmPaDestroy(0);
109 //MmTrDestroy();
110 //MmArchDestroy();
111 //MmPaDestroy(1);
112 goto Quickie;
113 }
114
115 /* Do we have too many descriptors? */
116 if (LibraryParameters->DescriptorCount > 512)
117 {
118 /* Switch to using a dynamic buffer instead */
119 EarlyPrint(L"Warning: too many descriptors\n");
120 Status = STATUS_NOT_IMPLEMENTED;
121 goto Quickie;
122 //MmMdpSwitchToDynamicDescriptors(LibraryParameters->DescriptorCount);
123 }
124
125 /* Remove memory that the BCD says is bad */
126 BlMmRemoveBadMemory();
127
128 /* Now map all the memory regions as needed */
129 Status = MmArchInitialize(2,
130 MemoryData,
131 TranslationType,
132 LibraryParameters->TranslationType);
133 if (NT_SUCCESS(Status))
134 {
135 /* Initialize the block allocator */
136 Status = MmBaInitialize();
137 }
138
139 /* Check if anything in phase 2 failed */
140 if (!NT_SUCCESS(Status))
141 {
142 /* Go back to static descriptors and kill the heap */
143 //MmMdpSwitchToStaticDescriptors();
144 //HapInitializationStatus = 0;
145 ++MmDescriptorCallTreeCount;
146
147 /* Destroy the Phase 1 initialization */
148 //MmPaDestroy(0);
149 //MmTrDestroy();
150 //MmArchDestroy();
151 //MmPaDestroy(1);
152 }
153
154 Quickie:
155 /* Free the memory descriptors and return the initialization state */
156 //MmMdFreeGlobalDescriptors();
157 --MmDescriptorCallTreeCount;
158 return Status;
159 }