Preparations for checked/free like builds (CPRINT == DbgPrint when DBG is defined).
[reactos.git] / reactos / ntoskrnl / mm / mminit.c
1 /* $Id: mminit.c,v 1.21 2001/05/01 23:08:20 chorns Exp $
2 *
3 * COPYRIGHT: See COPYING in the top directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/mm/mminit.c
6 * PURPOSE: kernel memory managment initialization functions
7 * PROGRAMMER: David Welch (welch@cwcom.net)
8 * UPDATE HISTORY:
9 * Created 9/4/98
10 */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <ddk/ntddk.h>
15 #include <internal/config.h>
16 #include <internal/i386/segment.h>
17 #include <internal/mm.h>
18 #include <internal/ntoskrnl.h>
19 #include <internal/bitops.h>
20 #include <internal/io.h>
21 #include <internal/ps.h>
22 #include <napi/shared_data.h>
23
24 #define NDEBUG
25 #include <internal/debug.h>
26
27 /* GLOBALS *****************************************************************/
28
29 /*
30 * Size of extended memory (kb) (fixed for now)
31 */
32 #define EXTENDED_MEMORY_SIZE (3*1024*1024)
33
34 /*
35 * Compiler defined symbols
36 */
37 extern unsigned int _text_start__;
38 extern unsigned int _text_end__;
39
40 static BOOLEAN IsThisAnNtAsSystem = FALSE;
41 static MM_SYSTEM_SIZE MmSystemSize = MmSmallSystem;
42
43 extern unsigned int _bss_end__;
44
45 static MEMORY_AREA* kernel_text_desc = NULL;
46 static MEMORY_AREA* kernel_data_desc = NULL;
47 static MEMORY_AREA* kernel_param_desc = NULL;
48 static MEMORY_AREA* kernel_pool_desc = NULL;
49 static MEMORY_AREA* kernel_shared_data_desc = NULL;
50
51 PVOID MmSharedDataPagePhysicalAddress = NULL;
52
53 /* FUNCTIONS ****************************************************************/
54
55 BOOLEAN STDCALL MmIsThisAnNtAsSystem(VOID)
56 {
57 return(IsThisAnNtAsSystem);
58 }
59
60 MM_SYSTEM_SIZE STDCALL MmQuerySystemSize(VOID)
61 {
62 return(MmSystemSize);
63 }
64
65 VOID MiShutdownMemoryManager(VOID)
66 {
67 }
68
69 VOID MmInitVirtualMemory(ULONG LastKernelAddress,
70 ULONG KernelLength)
71 /*
72 * FUNCTION: Intialize the memory areas list
73 * ARGUMENTS:
74 * bp = Pointer to the boot parameters
75 * kernel_len = Length of the kernel
76 */
77 {
78 PVOID BaseAddress;
79 ULONG Length;
80 ULONG ParamLength = KernelLength;
81 NTSTATUS Status;
82 //ULONG i;
83
84 DPRINT("MmInitVirtualMemory(%x, %x)\n",LastKernelAddress, KernelLength);
85
86 LastKernelAddress = PAGE_ROUND_UP(LastKernelAddress);
87
88 MmInitMemoryAreas();
89 // ExInitNonPagedPool(KERNEL_BASE + PAGE_ROUND_UP(kernel_len) + PAGESIZE);
90 ExInitNonPagedPool(LastKernelAddress + PAGESIZE);
91
92
93 /*
94 * Setup the system area descriptor list
95 */
96 BaseAddress = (PVOID)KERNEL_BASE;
97 Length = PAGE_ROUND_UP(((ULONG)&_text_end__)) - KERNEL_BASE;
98 ParamLength = ParamLength - Length;
99 MmCreateMemoryArea(NULL,
100 MmGetKernelAddressSpace(),
101 MEMORY_AREA_SYSTEM,
102 &BaseAddress,
103 Length,
104 0,
105 &kernel_text_desc,
106 FALSE);
107 Length = PAGE_ROUND_UP(((ULONG)&_bss_end__)) -
108 PAGE_ROUND_UP(((ULONG)&_text_end__));
109 ParamLength = ParamLength - Length;
110 DPRINT("Length %x\n",Length);
111 BaseAddress = (PVOID)PAGE_ROUND_UP(((ULONG)&_text_end__));
112 DPRINT("BaseAddress %x\n",BaseAddress);
113 MmCreateMemoryArea(NULL,
114 MmGetKernelAddressSpace(),
115 MEMORY_AREA_SYSTEM,
116 &BaseAddress,
117 Length,
118 0,
119 &kernel_data_desc,
120 FALSE);
121
122 BaseAddress = (PVOID)PAGE_ROUND_UP(((ULONG)&_bss_end__));
123 // Length = ParamLength;
124 Length = LastKernelAddress - (ULONG)BaseAddress;
125 MmCreateMemoryArea(NULL,
126 MmGetKernelAddressSpace(),
127 MEMORY_AREA_SYSTEM,
128 &BaseAddress,
129 Length,
130 0,
131 &kernel_param_desc,
132 FALSE);
133
134 BaseAddress = (PVOID)(LastKernelAddress + PAGESIZE);
135 Length = NONPAGED_POOL_SIZE;
136 MmCreateMemoryArea(NULL,
137 MmGetKernelAddressSpace(),
138 MEMORY_AREA_SYSTEM,
139 &BaseAddress,
140 Length,
141 0,
142 &kernel_pool_desc,
143 FALSE);
144
145 BaseAddress = (PVOID)KERNEL_SHARED_DATA_BASE;
146 Length = PAGESIZE;
147 MmCreateMemoryArea(NULL,
148 MmGetKernelAddressSpace(),
149 MEMORY_AREA_SYSTEM,
150 &BaseAddress,
151 Length,
152 0,
153 &kernel_shared_data_desc,
154 FALSE);
155
156 MmSharedDataPagePhysicalAddress = MmAllocPage(0);
157 Status = MmCreateVirtualMapping(NULL,
158 (PVOID)KERNEL_SHARED_DATA_BASE,
159 PAGE_READWRITE,
160 (ULONG)MmSharedDataPagePhysicalAddress);
161 if (!NT_SUCCESS(Status))
162 {
163 DbgPrint("Unable to create virtual mapping\n");
164 KeBugCheck(0);
165 }
166 ((PKUSER_SHARED_DATA)KERNEL_SHARED_DATA_BASE)->TickCountLow = 0xdeadbeef;
167 #if 0
168 for (i = 0; i < 0x100; i++)
169 {
170 Status = MmCreateVirtualMapping(NULL,
171 (PVOID)(i*PAGESIZE),
172 PAGE_READWRITE,
173 (ULONG)(i*PAGESIZE));
174 if (!NT_SUCCESS(Status))
175 {
176 DbgPrint("Unable to create virtual mapping\n");
177 KeBugCheck(0);
178 }
179 }
180 #endif
181 // MmDumpMemoryAreas();
182 DPRINT("MmInitVirtualMemory() done\n");
183 }
184
185 VOID MmInit1(ULONG FirstKrnlPhysAddr,
186 ULONG LastKrnlPhysAddr,
187 ULONG LastKernelAddress,
188 PADDRESS_RANGE BIOSMemoryMap,
189 ULONG AddressRangeCount)
190 /*
191 * FUNCTION: Initalize memory managment
192 */
193 {
194 ULONG i;
195 ULONG kernel_len;
196 #ifndef MP
197 extern unsigned int unmap_me, unmap_me2, unmap_me3;
198 #endif
199
200 DPRINT("MmInit1(FirstKrnlPhysAddr, %x, LastKrnlPhysAddr %x, LastKernelAddress %x)\n",
201 FirstKrnlPhysAddr,
202 LastKrnlPhysAddr,
203 LastKernelAddress);
204
205 /*
206 * FIXME: Set this based on the system command line
207 */
208 MmUserProbeAddress = (PVOID)0x7fff0000;
209 MmHighestUserAddress = (PVOID)0x7ffeffff;
210
211 /*
212 * Initialize memory managment statistics
213 */
214 MmStats.NrTotalPages = 0;
215 MmStats.NrSystemPages = 0;
216 MmStats.NrUserPages = 0;
217 MmStats.NrReservedPages = 0;
218 MmStats.NrUserPages = 0;
219 MmStats.NrFreePages = 0;
220 MmStats.NrLockedPages = 0;
221 MmStats.PagingRequestsInLastMinute = 0;
222 MmStats.PagingRequestsInLastFiveMinutes = 0;
223 MmStats.PagingRequestsInLastFifteenMinutes = 0;
224
225 /*
226 * Initialize the kernel address space
227 */
228 MmInitializeKernelAddressSpace();
229
230 /*
231 * Unmap low memory
232 */
233 #ifndef MP
234 /* FIXME: This is broken in SMP mode */
235 //MmDeletePageTable(NULL, 0);
236 #endif
237 /*
238 * Free all pages not used for kernel memory
239 * (we assume the kernel occupies a continuous range of physical
240 * memory)
241 */
242 DPRINT("first krnl %x\nlast krnl %x\n",FirstKrnlPhysAddr,
243 LastKrnlPhysAddr);
244
245 /*
246 * Free physical memory not used by the kernel
247 */
248 MmStats.NrTotalPages = KeLoaderBlock.MemHigher/4;
249 if (!MmStats.NrTotalPages)
250 {
251 DbgPrint("Memory not detected, default to 8 MB\n");
252 MmStats.NrTotalPages = 2048;
253 }
254 else
255 {
256 /* add 1MB for standard memory (not extended) */
257 MmStats.NrTotalPages += 256;
258 }
259 #if 1
260 MmStats.NrTotalPages += 16;
261 #endif
262 DbgPrint("Used memory %dKb\n", (MmStats.NrTotalPages * PAGESIZE) / 1024);
263
264 LastKernelAddress = (ULONG)MmInitializePageList(
265 (PVOID)FirstKrnlPhysAddr,
266 (PVOID)LastKrnlPhysAddr,
267 MmStats.NrTotalPages,
268 PAGE_ROUND_UP(LastKernelAddress),
269 BIOSMemoryMap,
270 AddressRangeCount);
271 kernel_len = LastKrnlPhysAddr - FirstKrnlPhysAddr;
272
273 /*
274 * Create a trap for null pointer references and protect text
275 * segment
276 */
277 CHECKPOINT;
278 DPRINT("_text_start__ %x _text_end__ %x\n",(int)&_text_start__,(int)&_text_end__);
279 for (i=PAGE_ROUND_UP(((int)&_text_start__));
280 i<PAGE_ROUND_DOWN(((int)&_text_end__));i=i+PAGESIZE)
281 {
282 MmSetPageProtect(NULL,
283 (PVOID)i,
284 PAGE_EXECUTE_READ);
285 }
286
287 DPRINT("Invalidating between %x and %x\n",
288 LastKernelAddress,
289 KERNEL_BASE + PAGE_TABLE_SIZE);
290 for (i=(LastKernelAddress);
291 i<(KERNEL_BASE + PAGE_TABLE_SIZE);
292 i=i+PAGESIZE)
293 {
294 MmDeleteVirtualMapping(NULL, (PVOID)(i), FALSE, NULL, NULL);
295 }
296 DPRINT("Almost done MmInit()\n");
297 #ifndef MP
298 /* FIXME: This is broken in SMP mode */
299 MmDeleteVirtualMapping(NULL, (PVOID)&unmap_me, FALSE, NULL, NULL);
300 MmDeleteVirtualMapping(NULL, (PVOID)&unmap_me2, FALSE, NULL, NULL);
301 MmDeleteVirtualMapping(NULL, (PVOID)&unmap_me3, FALSE, NULL, NULL);
302 #endif
303 /*
304 * Intialize memory areas
305 */
306 MmInitVirtualMemory(LastKernelAddress, kernel_len);
307 }
308
309 VOID MmInit2(VOID)
310 {
311 MmInitSectionImplementation();
312 MmInitPagingFile();
313 }
314
315 VOID MmInit3(VOID)
316 {
317 MmInitPagerThread();
318 MmCreatePhysicalMemorySection();
319
320 /* FIXME: Read parameters from memory */
321 }
322