- Initialize MmLowestPhysicalPage to -1, otherwise setting this value will never...
[reactos.git] / reactos / ntoskrnl / mm / mminit.c
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/mm/mminit.c
5 * PURPOSE: Memory Manager Initialization
6 * PROGRAMMERS:
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include <ntoskrnl.h>
12 #define NDEBUG
13 #include <debug.h>
14
15 /* GLOBALS *******************************************************************/
16
17 PCHAR
18 MemType[] =
19 {
20 "ExceptionBlock ",
21 "SystemBlock ",
22 "Free ",
23 "Bad ",
24 "LoadedProgram ",
25 "FirmwareTemporary ",
26 "FirmwarePermanent ",
27 "OsloaderHeap ",
28 "OsloaderStack ",
29 "SystemCode ",
30 "HalCode ",
31 "BootDriver ",
32 "ConsoleInDriver ",
33 "ConsoleOutDriver ",
34 "StartupDpcStack ",
35 "StartupKernelStack",
36 "StartupPanicStack ",
37 "StartupPcrPage ",
38 "StartupPdrPage ",
39 "RegistryData ",
40 "MemoryData ",
41 "NlsData ",
42 "SpecialMemory ",
43 "BBTMemory ",
44 "LoaderReserve ",
45 "LoaderXIPRom "
46 };
47
48 PVOID MiNonPagedPoolStart;
49 ULONG MiNonPagedPoolLength;
50 PBOOLEAN Mm64BitPhysicalAddress = FALSE;
51 ULONG MmReadClusterSize;
52 MM_STATS MmStats;
53 PMMSUPPORT MmKernelAddressSpace;
54 extern KMUTANT MmSystemLoadLock;
55 extern ULONG MmBootImageSize;
56 BOOLEAN MiDbgEnableMdDump =
57 #ifdef _ARM_
58 TRUE;
59 #else
60 FALSE;
61 #endif
62
63 /* PRIVATE FUNCTIONS *********************************************************/
64
65 VOID
66 INIT_FUNCTION
67 NTAPI
68 MiInitSystemMemoryAreas()
69 {
70 PVOID BaseAddress;
71 PHYSICAL_ADDRESS BoundaryAddressMultiple;
72 PMEMORY_AREA MArea;
73 BoundaryAddressMultiple.QuadPart = 0;
74
75 //
76 // First initialize the page table and hyperspace memory areas
77 //
78 MiInitPageDirectoryMap();
79
80 //
81 // Next, the KPCR
82 //
83 BaseAddress = (PVOID)PCR;
84 MmCreateMemoryArea(MmGetKernelAddressSpace(),
85 MEMORY_AREA_SYSTEM | MEMORY_AREA_STATIC,
86 &BaseAddress,
87 PAGE_SIZE * KeNumberProcessors,
88 PAGE_READWRITE,
89 &MArea,
90 TRUE,
91 0,
92 BoundaryAddressMultiple);
93
94 //
95 // Now the KUSER_SHARED_DATA
96 //
97 BaseAddress = (PVOID)KI_USER_SHARED_DATA;
98 MmCreateMemoryArea(MmGetKernelAddressSpace(),
99 MEMORY_AREA_SYSTEM | MEMORY_AREA_STATIC,
100 &BaseAddress,
101 PAGE_SIZE,
102 PAGE_READWRITE,
103 &MArea,
104 TRUE,
105 0,
106 BoundaryAddressMultiple);
107 }
108
109 VOID
110 NTAPI
111 MiDbgDumpMemoryDescriptors(VOID)
112 {
113 PLIST_ENTRY NextEntry;
114 PMEMORY_ALLOCATION_DESCRIPTOR Md;
115 ULONG TotalPages = 0;
116
117 DPRINT1("Base\t\tLength\t\tType\n");
118 for (NextEntry = KeLoaderBlock->MemoryDescriptorListHead.Flink;
119 NextEntry != &KeLoaderBlock->MemoryDescriptorListHead;
120 NextEntry = NextEntry->Flink)
121 {
122 Md = CONTAINING_RECORD(NextEntry, MEMORY_ALLOCATION_DESCRIPTOR, ListEntry);
123 DPRINT1("%08lX\t%08lX\t%s\n", Md->BasePage, Md->PageCount, MemType[Md->MemoryType]);
124 TotalPages += Md->PageCount;
125 }
126
127 DPRINT1("Total: %08lX (%d MB)\n", TotalPages, (TotalPages * PAGE_SIZE) / 1024 / 1024);
128 }
129
130 NTSTATUS
131 NTAPI
132 MmArmInitSystem(IN ULONG Phase,
133 IN PLOADER_PARAMETER_BLOCK LoaderBlock);
134
135 VOID
136 INIT_FUNCTION
137 NTAPI
138 MmInit1(VOID)
139 {
140 /* Initialize the kernel address space */
141 KeInitializeGuardedMutex(&PsGetCurrentProcess()->AddressCreationLock);
142 MmKernelAddressSpace = MmGetCurrentAddressSpace();
143 MmInitGlobalKernelPageDirectory();
144
145 /* Dump memory descriptors */
146 if (MiDbgEnableMdDump) MiDbgDumpMemoryDescriptors();
147
148 //
149 // Initialize ARM³ in phase 0
150 //
151 MmArmInitSystem(0, KeLoaderBlock);
152
153 /* Intialize system memory areas */
154 MiInitSystemMemoryAreas();
155
156 /* Initialize the page list */
157 MmInitializePageList();
158
159 //
160 // Initialize ARM³ in phase 1
161 //
162 MmArmInitSystem(1, KeLoaderBlock);
163 // DEPRECATED
164 /* Put nonpaged pool after the loaded modules */ // DEPRECATED
165 MiNonPagedPoolStart = (PVOID)((ULONG_PTR)MmSystemRangeStart + // DEPRECATED
166 MmBootImageSize); // DEPRECATED
167 MiNonPagedPoolLength = MM_NONPAGED_POOL_SIZE; // DEPRECATED
168 // DEPRECATED
169 /* Initialize nonpaged pool */ // DEPRECATED
170 MiInitializeNonPagedPool(); // DEPRECATED
171 // DEPRECATED
172 /* Put the paged pool after nonpaged pool */
173 MmPagedPoolBase = (PVOID)PAGE_ROUND_UP((ULONG_PTR)MiNonPagedPoolStart +
174 MiNonPagedPoolLength);
175 MmPagedPoolSize = MM_PAGED_POOL_SIZE;
176
177 //
178 // Initialize ARM³ in phase 2
179 //
180 MmArmInitSystem(2, KeLoaderBlock);
181
182 /* Initialize paged pool */
183 MmInitializePagedPool();
184
185 /* Initialize working sets */
186 MmInitializeMemoryConsumer(MC_USER, MmTrimUserMemory);
187 }
188
189 BOOLEAN
190 NTAPI
191 MmInitSystem(IN ULONG Phase,
192 IN PLOADER_PARAMETER_BLOCK LoaderBlock)
193 {
194 if (Phase == 0)
195 {
196 /* Initialize Mm bootstrap */
197 MmInit1();
198
199 /* Initialize the Loader Lock */
200 KeInitializeMutant(&MmSystemLoadLock, FALSE);
201
202 /* Reload boot drivers */
203 MiReloadBootLoadedDrivers(LoaderBlock);
204
205 /* Initialize the loaded module list */
206 MiInitializeLoadedModuleList(LoaderBlock);
207
208 /* We're done, for now */
209 DPRINT("Mm0: COMPLETE\n");
210 }
211 else if (Phase == 1)
212 {
213 MmInitializeRmapList();
214 MmInitializePageOp();
215 MmInitSectionImplementation();
216 MmInitPagingFile();
217 MmCreatePhysicalMemorySection();
218
219 /* Setup shared user data settings that NT does as well */
220 ASSERT(SharedUserData->NumberOfPhysicalPages == 0);
221 SharedUserData->NumberOfPhysicalPages = MmStats.NrTotalPages;
222 SharedUserData->LargePageMinimum = 0;
223
224 /* For now, we assume that we're always Workstation */
225 SharedUserData->NtProductType = NtProductWinNt;
226 }
227 else if (Phase == 2)
228 {
229 /*
230 * Unmap low memory
231 */
232 MiInitBalancerThread();
233
234 /*
235 * Initialise the modified page writer.
236 */
237 MmInitMpwThread();
238
239 /* Initialize the balance set manager */
240 MmInitBsmThread();
241
242 /* FIXME: Read parameters from memory */
243 }
244
245 return TRUE;
246 }
247