1 /* $Id: mminit.c,v 1.30 2002/01/01 00:21:56 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/io.h>
20 #include <internal/ps.h>
21 #include <napi/shared_data.h>
22 #include <internal/pool.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
;
50 static MEMORY_AREA
* MiPagedPoolDescriptor
= NULL
;
52 PVOID MmSharedDataPagePhysicalAddress
= NULL
;
54 /* FUNCTIONS ****************************************************************/
56 BOOLEAN STDCALL
MmIsThisAnNtAsSystem(VOID
)
58 return(IsThisAnNtAsSystem
);
61 MM_SYSTEM_SIZE STDCALL
MmQuerySystemSize(VOID
)
66 VOID
MiShutdownMemoryManager(VOID
)
70 VOID
MmInitVirtualMemory(ULONG LastKernelAddress
,
73 * FUNCTION: Intialize the memory areas list
75 * bp = Pointer to the boot parameters
76 * kernel_len = Length of the kernel
81 ULONG ParamLength
= KernelLength
;
85 DPRINT("MmInitVirtualMemory(%x, %x)\n",LastKernelAddress
, KernelLength
);
87 LastKernelAddress
= PAGE_ROUND_UP(LastKernelAddress
);
90 ExInitNonPagedPool(LastKernelAddress
+ PAGESIZE
);
93 * Setup the system area descriptor list
95 BaseAddress
= (PVOID
)KERNEL_BASE
;
96 Length
= PAGE_ROUND_UP(((ULONG
)&_text_end__
)) - KERNEL_BASE
;
97 ParamLength
= ParamLength
- Length
;
100 * No need to lock the address space at this point since no
101 * other threads are running.
103 MmCreateMemoryArea(NULL
,
104 MmGetKernelAddressSpace(),
112 Length
= PAGE_ROUND_UP(((ULONG
)&_bss_end__
)) -
113 PAGE_ROUND_UP(((ULONG
)&_text_end__
));
114 ParamLength
= ParamLength
- Length
;
115 DPRINT("Length %x\n",Length
);
116 BaseAddress
= (PVOID
)PAGE_ROUND_UP(((ULONG
)&_text_end__
));
117 DPRINT("BaseAddress %x\n",BaseAddress
);
120 * No need to lock the address space at this point since we are
121 * the only thread running.
123 MmCreateMemoryArea(NULL
,
124 MmGetKernelAddressSpace(),
132 BaseAddress
= (PVOID
)PAGE_ROUND_UP(((ULONG
)&_bss_end__
));
133 // Length = ParamLength;
134 Length
= LastKernelAddress
- (ULONG
)BaseAddress
;
135 MmCreateMemoryArea(NULL
,
136 MmGetKernelAddressSpace(),
144 BaseAddress
= (PVOID
)(LastKernelAddress
+ PAGESIZE
);
145 Length
= NONPAGED_POOL_SIZE
;
146 MmCreateMemoryArea(NULL
,
147 MmGetKernelAddressSpace(),
155 MmPagedPoolSize
= MM_PAGED_POOL_SIZE
;
156 BaseAddress
= (PVOID
)(LastKernelAddress
+ PAGESIZE
+ NONPAGED_POOL_SIZE
+
158 MmPagedPoolBase
= BaseAddress
;
159 Length
= MM_PAGED_POOL_SIZE
;
160 MmCreateMemoryArea(NULL
,
161 MmGetKernelAddressSpace(),
162 MEMORY_AREA_PAGED_POOL
,
166 &MiPagedPoolDescriptor
,
168 MmInitializePagedPool();
171 * Create the kernel mapping of the user/kernel shared memory.
173 BaseAddress
= (PVOID
)KERNEL_SHARED_DATA_BASE
;
175 MmCreateMemoryArea(NULL
,
176 MmGetKernelAddressSpace(),
181 &kernel_shared_data_desc
,
183 Status
= MmRequestPageMemoryConsumer(MC_NPPOOL
, TRUE
, &MmSharedDataPagePhysicalAddress
);
184 Status
= MmCreateVirtualMapping(NULL
,
185 (PVOID
)KERNEL_SHARED_DATA_BASE
,
187 (ULONG
)MmSharedDataPagePhysicalAddress
,
189 if (!NT_SUCCESS(Status
))
191 DbgPrint("Unable to create virtual mapping\n");
194 ((PKUSER_SHARED_DATA
)KERNEL_SHARED_DATA_BASE
)->TickCountLow
= 0xdeadbeef;
199 MmInitializeMemoryConsumer(MC_USER
, MmTrimUserMemory
);
202 VOID
MmInit1(ULONG FirstKrnlPhysAddr
,
203 ULONG LastKrnlPhysAddr
,
204 ULONG LastKernelAddress
,
205 PADDRESS_RANGE BIOSMemoryMap
,
206 ULONG AddressRangeCount
)
208 * FUNCTION: Initalize memory managment
214 extern unsigned int unmap_me
, unmap_me2
, unmap_me3
;
217 DPRINT("MmInit1(FirstKrnlPhysAddr, %x, LastKrnlPhysAddr %x, LastKernelAddress %x)\n",
222 if ((BIOSMemoryMap
!= NULL
) && (AddressRangeCount
> 0))
224 // If we have a bios memory map, recalulate the the memory size
226 for (i
= 0; i
< AddressRangeCount
; i
++)
228 if (BIOSMemoryMap
[i
].Type
== 1
229 && (BIOSMemoryMap
[i
].BaseAddrLow
+ BIOSMemoryMap
[i
].LengthLow
+ PAGESIZE
-1) / PAGESIZE
> last
)
231 last
= (BIOSMemoryMap
[i
].BaseAddrLow
+ BIOSMemoryMap
[i
].LengthLow
+ PAGESIZE
-1) / PAGESIZE
;
234 if ((last
- 256) * 4 > KeLoaderBlock
.MemHigher
)
236 KeLoaderBlock
.MemHigher
= (last
- 256) * 4;
241 * FIXME: Set this based on the system command line
243 MmUserProbeAddress
= (PVOID
)0x7fff0000;
244 MmHighestUserAddress
= (PVOID
)0x7ffeffff;
247 * Initialize memory managment statistics
249 MmStats
.NrTotalPages
= 0;
250 MmStats
.NrSystemPages
= 0;
251 MmStats
.NrUserPages
= 0;
252 MmStats
.NrReservedPages
= 0;
253 MmStats
.NrUserPages
= 0;
254 MmStats
.NrFreePages
= 0;
255 MmStats
.NrLockedPages
= 0;
256 MmStats
.PagingRequestsInLastMinute
= 0;
257 MmStats
.PagingRequestsInLastFiveMinutes
= 0;
258 MmStats
.PagingRequestsInLastFifteenMinutes
= 0;
261 * Initialize the kernel address space
263 MmInitializeKernelAddressSpace();
269 /* FIXME: This is broken in SMP mode */
270 //MmDeletePageTable(NULL, 0);
273 * Free all pages not used for kernel memory
274 * (we assume the kernel occupies a continuous range of physical
277 DPRINT("first krnl %x\nlast krnl %x\n",FirstKrnlPhysAddr
,
281 * Free physical memory not used by the kernel
283 MmStats
.NrTotalPages
= KeLoaderBlock
.MemHigher
/4;
284 if (!MmStats
.NrTotalPages
)
286 DbgPrint("Memory not detected, default to 8 MB\n");
287 MmStats
.NrTotalPages
= 2048;
291 /* add 1MB for standard memory (not extended) */
292 MmStats
.NrTotalPages
+= 256;
295 MmStats
.NrTotalPages
+= 16;
297 DbgPrint("Used memory %dKb\n", (MmStats
.NrTotalPages
* PAGESIZE
) / 1024);
299 LastKernelAddress
= (ULONG
)MmInitializePageList(
300 (PVOID
)FirstKrnlPhysAddr
,
301 (PVOID
)LastKrnlPhysAddr
,
302 MmStats
.NrTotalPages
,
303 PAGE_ROUND_UP(LastKernelAddress
),
306 kernel_len
= LastKrnlPhysAddr
- FirstKrnlPhysAddr
;
309 * Create a trap for null pointer references and protect text
313 DPRINT("_text_start__ %x _text_end__ %x\n",(int)&_text_start__
,(int)&_text_end__
);
314 for (i
=PAGE_ROUND_UP(((int)&_text_start__
));
315 i
<PAGE_ROUND_DOWN(((int)&_text_end__
));i
=i
+PAGESIZE
)
317 MmSetPageProtect(NULL
,
322 DPRINT("Invalidating between %x and %x\n",
324 KERNEL_BASE
+ PAGE_TABLE_SIZE
);
325 for (i
=(LastKernelAddress
);
326 i
<(KERNEL_BASE
+ PAGE_TABLE_SIZE
);
329 MmDeleteVirtualMapping(NULL
, (PVOID
)(i
), FALSE
, NULL
, NULL
);
331 DPRINT("Almost done MmInit()\n");
333 /* FIXME: This is broken in SMP mode */
334 MmDeleteVirtualMapping(NULL
, (PVOID
)&unmap_me
, FALSE
, NULL
, NULL
);
335 MmDeleteVirtualMapping(NULL
, (PVOID
)&unmap_me2
, FALSE
, NULL
, NULL
);
336 MmDeleteVirtualMapping(NULL
, (PVOID
)&unmap_me3
, FALSE
, NULL
, NULL
);
339 * Intialize memory areas
341 MmInitVirtualMemory(LastKernelAddress
, kernel_len
);
346 MmInitSectionImplementation();
353 MmCreatePhysicalMemorySection();
354 MmInitializeRmapList();
356 /* FIXME: Read parameters from memory */