1 /* $Id: mminit.c,v 1.26 2001/11/25 15:21:11 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
;
101 * No need to lock the address space at this point since no
102 * other threads are running.
104 MmCreateMemoryArea(NULL
,
105 MmGetKernelAddressSpace(),
113 Length
= PAGE_ROUND_UP(((ULONG
)&_bss_end__
)) -
114 PAGE_ROUND_UP(((ULONG
)&_text_end__
));
115 ParamLength
= ParamLength
- Length
;
116 DPRINT("Length %x\n",Length
);
117 BaseAddress
= (PVOID
)PAGE_ROUND_UP(((ULONG
)&_text_end__
));
118 DPRINT("BaseAddress %x\n",BaseAddress
);
121 * No need to lock the address space at this point since we are
122 * the only thread running.
124 MmCreateMemoryArea(NULL
,
125 MmGetKernelAddressSpace(),
133 BaseAddress
= (PVOID
)PAGE_ROUND_UP(((ULONG
)&_bss_end__
));
134 // Length = ParamLength;
135 Length
= LastKernelAddress
- (ULONG
)BaseAddress
;
136 MmCreateMemoryArea(NULL
,
137 MmGetKernelAddressSpace(),
145 BaseAddress
= (PVOID
)(LastKernelAddress
+ PAGESIZE
);
146 Length
= NONPAGED_POOL_SIZE
;
147 MmCreateMemoryArea(NULL
,
148 MmGetKernelAddressSpace(),
157 * Create the kernel mapping of the user/kernel shared memory.
159 BaseAddress
= (PVOID
)KERNEL_SHARED_DATA_BASE
;
161 MmCreateMemoryArea(NULL
,
162 MmGetKernelAddressSpace(),
167 &kernel_shared_data_desc
,
169 MmSharedDataPagePhysicalAddress
= MmAllocPage(0);
170 Status
= MmCreateVirtualMapping(NULL
,
171 (PVOID
)KERNEL_SHARED_DATA_BASE
,
173 (ULONG
)MmSharedDataPagePhysicalAddress
);
174 if (!NT_SUCCESS(Status
))
176 DbgPrint("Unable to create virtual mapping\n");
179 ((PKUSER_SHARED_DATA
)KERNEL_SHARED_DATA_BASE
)->TickCountLow
= 0xdeadbeef;
182 VOID
MmInit1(ULONG FirstKrnlPhysAddr
,
183 ULONG LastKrnlPhysAddr
,
184 ULONG LastKernelAddress
,
185 PADDRESS_RANGE BIOSMemoryMap
,
186 ULONG AddressRangeCount
)
188 * FUNCTION: Initalize memory managment
194 extern unsigned int unmap_me
, unmap_me2
, unmap_me3
;
197 DPRINT("MmInit1(FirstKrnlPhysAddr, %x, LastKrnlPhysAddr %x, LastKernelAddress %x)\n",
202 if ((BIOSMemoryMap
!= NULL
) && (AddressRangeCount
> 0))
204 // If we have a bios memory map, recalulate the the memory size
206 for (i
= 0; i
< AddressRangeCount
; i
++)
208 if (BIOSMemoryMap
[i
].Type
== 1
209 && (BIOSMemoryMap
[i
].BaseAddrLow
+ BIOSMemoryMap
[i
].LengthLow
+ PAGESIZE
-1) / PAGESIZE
> last
)
211 last
= (BIOSMemoryMap
[i
].BaseAddrLow
+ BIOSMemoryMap
[i
].LengthLow
+ PAGESIZE
-1) / PAGESIZE
;
214 if ((last
- 256) * 4 > KeLoaderBlock
.MemHigher
)
216 KeLoaderBlock
.MemHigher
= (last
- 256) * 4;
221 * FIXME: Set this based on the system command line
223 MmUserProbeAddress
= (PVOID
)0x7fff0000;
224 MmHighestUserAddress
= (PVOID
)0x7ffeffff;
227 * Initialize memory managment statistics
229 MmStats
.NrTotalPages
= 0;
230 MmStats
.NrSystemPages
= 0;
231 MmStats
.NrUserPages
= 0;
232 MmStats
.NrReservedPages
= 0;
233 MmStats
.NrUserPages
= 0;
234 MmStats
.NrFreePages
= 0;
235 MmStats
.NrLockedPages
= 0;
236 MmStats
.PagingRequestsInLastMinute
= 0;
237 MmStats
.PagingRequestsInLastFiveMinutes
= 0;
238 MmStats
.PagingRequestsInLastFifteenMinutes
= 0;
241 * Initialize the kernel address space
243 MmInitializeKernelAddressSpace();
249 /* FIXME: This is broken in SMP mode */
250 //MmDeletePageTable(NULL, 0);
253 * Free all pages not used for kernel memory
254 * (we assume the kernel occupies a continuous range of physical
257 DPRINT("first krnl %x\nlast krnl %x\n",FirstKrnlPhysAddr
,
261 * Free physical memory not used by the kernel
263 MmStats
.NrTotalPages
= KeLoaderBlock
.MemHigher
/4;
264 if (!MmStats
.NrTotalPages
)
266 DbgPrint("Memory not detected, default to 8 MB\n");
267 MmStats
.NrTotalPages
= 2048;
271 /* add 1MB for standard memory (not extended) */
272 MmStats
.NrTotalPages
+= 256;
275 MmStats
.NrTotalPages
+= 16;
277 DbgPrint("Used memory %dKb\n", (MmStats
.NrTotalPages
* PAGESIZE
) / 1024);
279 LastKernelAddress
= (ULONG
)MmInitializePageList(
280 (PVOID
)FirstKrnlPhysAddr
,
281 (PVOID
)LastKrnlPhysAddr
,
282 MmStats
.NrTotalPages
,
283 PAGE_ROUND_UP(LastKernelAddress
),
286 kernel_len
= LastKrnlPhysAddr
- FirstKrnlPhysAddr
;
289 * Create a trap for null pointer references and protect text
293 DPRINT("_text_start__ %x _text_end__ %x\n",(int)&_text_start__
,(int)&_text_end__
);
294 for (i
=PAGE_ROUND_UP(((int)&_text_start__
));
295 i
<PAGE_ROUND_DOWN(((int)&_text_end__
));i
=i
+PAGESIZE
)
297 MmSetPageProtect(NULL
,
302 DPRINT("Invalidating between %x and %x\n",
304 KERNEL_BASE
+ PAGE_TABLE_SIZE
);
305 for (i
=(LastKernelAddress
);
306 i
<(KERNEL_BASE
+ PAGE_TABLE_SIZE
);
309 MmDeleteVirtualMapping(NULL
, (PVOID
)(i
), FALSE
, NULL
, NULL
);
311 DPRINT("Almost done MmInit()\n");
313 /* FIXME: This is broken in SMP mode */
314 MmDeleteVirtualMapping(NULL
, (PVOID
)&unmap_me
, FALSE
, NULL
, NULL
);
315 MmDeleteVirtualMapping(NULL
, (PVOID
)&unmap_me2
, FALSE
, NULL
, NULL
);
316 MmDeleteVirtualMapping(NULL
, (PVOID
)&unmap_me3
, FALSE
, NULL
, NULL
);
319 * Intialize memory areas
321 MmInitVirtualMemory(LastKernelAddress
, kernel_len
);
326 MmInitSectionImplementation();
333 MmCreatePhysicalMemorySection();
335 /* FIXME: Read parameters from memory */