1 /* $Id: mminit.c,v 1.25 2001/08/30 20:38:20 dwelch Exp $
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)
12 /* INCLUDES *****************************************************************/
14 #include <ddk/ntddk.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>
25 #include <internal/debug.h>
27 /* GLOBALS *****************************************************************/
30 * Size of extended memory (kb) (fixed for now)
32 #define EXTENDED_MEMORY_SIZE (3*1024*1024)
35 * Compiler defined symbols
37 extern unsigned int _text_start__
;
38 extern unsigned int _text_end__
;
40 static BOOLEAN IsThisAnNtAsSystem
= FALSE
;
41 static MM_SYSTEM_SIZE MmSystemSize
= MmSmallSystem
;
43 extern unsigned int _bss_end__
;
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
;
51 PVOID MmSharedDataPagePhysicalAddress
= NULL
;
53 /* FUNCTIONS ****************************************************************/
55 BOOLEAN STDCALL
MmIsThisAnNtAsSystem(VOID
)
57 return(IsThisAnNtAsSystem
);
60 MM_SYSTEM_SIZE STDCALL
MmQuerySystemSize(VOID
)
65 VOID
MiShutdownMemoryManager(VOID
)
69 VOID
MmInitVirtualMemory(ULONG LastKernelAddress
,
72 * FUNCTION: Intialize the memory areas list
74 * bp = Pointer to the boot parameters
75 * kernel_len = Length of the kernel
80 ULONG ParamLength
= KernelLength
;
84 DPRINT("MmInitVirtualMemory(%x, %x)\n",LastKernelAddress
, KernelLength
);
86 LastKernelAddress
= PAGE_ROUND_UP(LastKernelAddress
);
89 // ExInitNonPagedPool(KERNEL_BASE + PAGE_ROUND_UP(kernel_len) + PAGESIZE);
90 ExInitNonPagedPool(LastKernelAddress
+ PAGESIZE
);
94 * Setup the system area descriptor list
96 BaseAddress
= (PVOID
)KERNEL_BASE
;
97 Length
= PAGE_ROUND_UP(((ULONG
)&_text_end__
)) - KERNEL_BASE
;
98 ParamLength
= ParamLength
- Length
;
100 MmLockAddressSpace(MmGetKernelAddressSpace());
101 MmCreateMemoryArea(NULL
,
102 MmGetKernelAddressSpace(),
109 MmUnlockAddressSpace(MmGetKernelAddressSpace());
111 Length
= PAGE_ROUND_UP(((ULONG
)&_bss_end__
)) -
112 PAGE_ROUND_UP(((ULONG
)&_text_end__
));
113 ParamLength
= ParamLength
- Length
;
114 DPRINT("Length %x\n",Length
);
115 BaseAddress
= (PVOID
)PAGE_ROUND_UP(((ULONG
)&_text_end__
));
116 DPRINT("BaseAddress %x\n",BaseAddress
);
117 MmLockAddressSpace(MmGetKernelAddressSpace());
118 MmCreateMemoryArea(NULL
,
119 MmGetKernelAddressSpace(),
127 BaseAddress
= (PVOID
)PAGE_ROUND_UP(((ULONG
)&_bss_end__
));
128 // Length = ParamLength;
129 Length
= LastKernelAddress
- (ULONG
)BaseAddress
;
130 MmCreateMemoryArea(NULL
,
131 MmGetKernelAddressSpace(),
139 BaseAddress
= (PVOID
)(LastKernelAddress
+ PAGESIZE
);
140 Length
= NONPAGED_POOL_SIZE
;
141 MmCreateMemoryArea(NULL
,
142 MmGetKernelAddressSpace(),
150 BaseAddress
= (PVOID
)KERNEL_SHARED_DATA_BASE
;
152 MmCreateMemoryArea(NULL
,
153 MmGetKernelAddressSpace(),
158 &kernel_shared_data_desc
,
160 MmUnlockAddressSpace(MmGetKernelAddressSpace());
161 MmSharedDataPagePhysicalAddress
= MmAllocPage(0);
162 Status
= MmCreateVirtualMapping(NULL
,
163 (PVOID
)KERNEL_SHARED_DATA_BASE
,
165 (ULONG
)MmSharedDataPagePhysicalAddress
);
166 if (!NT_SUCCESS(Status
))
168 DbgPrint("Unable to create virtual mapping\n");
171 ((PKUSER_SHARED_DATA
)KERNEL_SHARED_DATA_BASE
)->TickCountLow
= 0xdeadbeef;
173 for (i
= 0; i
< 0x100; i
++)
175 Status
= MmCreateVirtualMapping(NULL
,
178 (ULONG
)(i
*PAGESIZE
));
179 if (!NT_SUCCESS(Status
))
181 DbgPrint("Unable to create virtual mapping\n");
186 // MmDumpMemoryAreas();
187 DPRINT("MmInitVirtualMemory() done\n");
190 VOID
MmInit1(ULONG FirstKrnlPhysAddr
,
191 ULONG LastKrnlPhysAddr
,
192 ULONG LastKernelAddress
,
193 PADDRESS_RANGE BIOSMemoryMap
,
194 ULONG AddressRangeCount
)
196 * FUNCTION: Initalize memory managment
202 extern unsigned int unmap_me
, unmap_me2
, unmap_me3
;
205 DPRINT("MmInit1(FirstKrnlPhysAddr, %x, LastKrnlPhysAddr %x, LastKernelAddress %x)\n",
210 if ((BIOSMemoryMap
!= NULL
) && (AddressRangeCount
> 0))
212 // If we have a bios memory map, recalulate the the memory size
214 for (i
= 0; i
< AddressRangeCount
; i
++)
216 if (BIOSMemoryMap
[i
].Type
== 1
217 && (BIOSMemoryMap
[i
].BaseAddrLow
+ BIOSMemoryMap
[i
].LengthLow
+ PAGESIZE
-1) / PAGESIZE
> last
)
219 last
= (BIOSMemoryMap
[i
].BaseAddrLow
+ BIOSMemoryMap
[i
].LengthLow
+ PAGESIZE
-1) / PAGESIZE
;
222 if ((last
- 256) * 4 > KeLoaderBlock
.MemHigher
)
224 KeLoaderBlock
.MemHigher
= (last
- 256) * 4;
229 * FIXME: Set this based on the system command line
231 MmUserProbeAddress
= (PVOID
)0x7fff0000;
232 MmHighestUserAddress
= (PVOID
)0x7ffeffff;
235 * Initialize memory managment statistics
237 MmStats
.NrTotalPages
= 0;
238 MmStats
.NrSystemPages
= 0;
239 MmStats
.NrUserPages
= 0;
240 MmStats
.NrReservedPages
= 0;
241 MmStats
.NrUserPages
= 0;
242 MmStats
.NrFreePages
= 0;
243 MmStats
.NrLockedPages
= 0;
244 MmStats
.PagingRequestsInLastMinute
= 0;
245 MmStats
.PagingRequestsInLastFiveMinutes
= 0;
246 MmStats
.PagingRequestsInLastFifteenMinutes
= 0;
249 * Initialize the kernel address space
251 MmInitializeKernelAddressSpace();
257 /* FIXME: This is broken in SMP mode */
258 //MmDeletePageTable(NULL, 0);
261 * Free all pages not used for kernel memory
262 * (we assume the kernel occupies a continuous range of physical
265 DPRINT("first krnl %x\nlast krnl %x\n",FirstKrnlPhysAddr
,
269 * Free physical memory not used by the kernel
271 MmStats
.NrTotalPages
= KeLoaderBlock
.MemHigher
/4;
272 if (!MmStats
.NrTotalPages
)
274 DbgPrint("Memory not detected, default to 8 MB\n");
275 MmStats
.NrTotalPages
= 2048;
279 /* add 1MB for standard memory (not extended) */
280 MmStats
.NrTotalPages
+= 256;
283 MmStats
.NrTotalPages
+= 16;
285 DbgPrint("Used memory %dKb\n", (MmStats
.NrTotalPages
* PAGESIZE
) / 1024);
287 LastKernelAddress
= (ULONG
)MmInitializePageList(
288 (PVOID
)FirstKrnlPhysAddr
,
289 (PVOID
)LastKrnlPhysAddr
,
290 MmStats
.NrTotalPages
,
291 PAGE_ROUND_UP(LastKernelAddress
),
294 kernel_len
= LastKrnlPhysAddr
- FirstKrnlPhysAddr
;
297 * Create a trap for null pointer references and protect text
301 DPRINT("_text_start__ %x _text_end__ %x\n",(int)&_text_start__
,(int)&_text_end__
);
302 for (i
=PAGE_ROUND_UP(((int)&_text_start__
));
303 i
<PAGE_ROUND_DOWN(((int)&_text_end__
));i
=i
+PAGESIZE
)
305 MmSetPageProtect(NULL
,
310 DPRINT("Invalidating between %x and %x\n",
312 KERNEL_BASE
+ PAGE_TABLE_SIZE
);
313 for (i
=(LastKernelAddress
);
314 i
<(KERNEL_BASE
+ PAGE_TABLE_SIZE
);
317 MmDeleteVirtualMapping(NULL
, (PVOID
)(i
), FALSE
, NULL
, NULL
);
319 DPRINT("Almost done MmInit()\n");
321 /* FIXME: This is broken in SMP mode */
322 MmDeleteVirtualMapping(NULL
, (PVOID
)&unmap_me
, FALSE
, NULL
, NULL
);
323 MmDeleteVirtualMapping(NULL
, (PVOID
)&unmap_me2
, FALSE
, NULL
, NULL
);
324 MmDeleteVirtualMapping(NULL
, (PVOID
)&unmap_me3
, FALSE
, NULL
, NULL
);
327 * Intialize memory areas
329 MmInitVirtualMemory(LastKernelAddress
, kernel_len
);
334 MmInitSectionImplementation();
341 MmCreatePhysicalMemorySection();
343 /* FIXME: Read parameters from memory */