1 /* $Id: mminit.c,v 1.31 2002/04/26 13:11:55 ekohl 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 <internal/pool.h>
24 #include <internal/debug.h>
26 /* GLOBALS *****************************************************************/
29 * Size of extended memory (kb) (fixed for now)
31 #define EXTENDED_MEMORY_SIZE (3*1024*1024)
34 * Compiler defined symbols
36 extern unsigned int _text_start__
;
37 extern unsigned int _text_end__
;
39 static BOOLEAN IsThisAnNtAsSystem
= FALSE
;
40 static MM_SYSTEM_SIZE MmSystemSize
= MmSmallSystem
;
42 extern unsigned int _bss_end__
;
44 static MEMORY_AREA
* kernel_text_desc
= NULL
;
45 static MEMORY_AREA
* kernel_data_desc
= NULL
;
46 static MEMORY_AREA
* kernel_param_desc
= NULL
;
47 static MEMORY_AREA
* kernel_pool_desc
= NULL
;
48 static MEMORY_AREA
* kernel_shared_data_desc
= NULL
;
49 static MEMORY_AREA
* MiPagedPoolDescriptor
= 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(LastKernelAddress
+ PAGESIZE
);
92 * Setup the system area descriptor list
94 BaseAddress
= (PVOID
)KERNEL_BASE
;
95 Length
= PAGE_ROUND_UP(((ULONG
)&_text_end__
)) - KERNEL_BASE
;
96 ParamLength
= ParamLength
- Length
;
99 * No need to lock the address space at this point since no
100 * other threads are running.
102 MmCreateMemoryArea(NULL
,
103 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
);
119 * No need to lock the address space at this point since we are
120 * the only thread running.
122 MmCreateMemoryArea(NULL
,
123 MmGetKernelAddressSpace(),
131 BaseAddress
= (PVOID
)PAGE_ROUND_UP(((ULONG
)&_bss_end__
));
132 // Length = ParamLength;
133 Length
= LastKernelAddress
- (ULONG
)BaseAddress
;
134 MmCreateMemoryArea(NULL
,
135 MmGetKernelAddressSpace(),
143 BaseAddress
= (PVOID
)(LastKernelAddress
+ PAGESIZE
);
144 Length
= NONPAGED_POOL_SIZE
;
145 MmCreateMemoryArea(NULL
,
146 MmGetKernelAddressSpace(),
154 MmPagedPoolSize
= MM_PAGED_POOL_SIZE
;
155 BaseAddress
= (PVOID
)(LastKernelAddress
+ PAGESIZE
+ NONPAGED_POOL_SIZE
+
157 MmPagedPoolBase
= BaseAddress
;
158 Length
= MM_PAGED_POOL_SIZE
;
159 MmCreateMemoryArea(NULL
,
160 MmGetKernelAddressSpace(),
161 MEMORY_AREA_PAGED_POOL
,
165 &MiPagedPoolDescriptor
,
167 MmInitializePagedPool();
170 * Create the kernel mapping of the user/kernel shared memory.
172 BaseAddress
= (PVOID
)KI_USER_SHARED_DATA
;
174 MmCreateMemoryArea(NULL
,
175 MmGetKernelAddressSpace(),
180 &kernel_shared_data_desc
,
182 Status
= MmRequestPageMemoryConsumer(MC_NPPOOL
, TRUE
, &MmSharedDataPagePhysicalAddress
);
183 Status
= MmCreateVirtualMapping(NULL
,
184 (PVOID
)KI_USER_SHARED_DATA
,
186 (ULONG
)MmSharedDataPagePhysicalAddress
,
188 if (!NT_SUCCESS(Status
))
190 DbgPrint("Unable to create virtual mapping\n");
193 RtlZeroMemory(BaseAddress
, Length
);
198 MmInitializeMemoryConsumer(MC_USER
, MmTrimUserMemory
);
201 VOID
MmInit1(ULONG FirstKrnlPhysAddr
,
202 ULONG LastKrnlPhysAddr
,
203 ULONG LastKernelAddress
,
204 PADDRESS_RANGE BIOSMemoryMap
,
205 ULONG AddressRangeCount
)
207 * FUNCTION: Initalize memory managment
213 extern unsigned int unmap_me
, unmap_me2
, unmap_me3
;
216 DPRINT("MmInit1(FirstKrnlPhysAddr, %x, LastKrnlPhysAddr %x, LastKernelAddress %x)\n",
221 if ((BIOSMemoryMap
!= NULL
) && (AddressRangeCount
> 0))
223 // If we have a bios memory map, recalulate the the memory size
225 for (i
= 0; i
< AddressRangeCount
; i
++)
227 if (BIOSMemoryMap
[i
].Type
== 1
228 && (BIOSMemoryMap
[i
].BaseAddrLow
+ BIOSMemoryMap
[i
].LengthLow
+ PAGESIZE
-1) / PAGESIZE
> last
)
230 last
= (BIOSMemoryMap
[i
].BaseAddrLow
+ BIOSMemoryMap
[i
].LengthLow
+ PAGESIZE
-1) / PAGESIZE
;
233 if ((last
- 256) * 4 > KeLoaderBlock
.MemHigher
)
235 KeLoaderBlock
.MemHigher
= (last
- 256) * 4;
240 * FIXME: Set this based on the system command line
242 MmUserProbeAddress
= (PVOID
)0x7fff0000;
243 MmHighestUserAddress
= (PVOID
)0x7ffeffff;
246 * Initialize memory managment statistics
248 MmStats
.NrTotalPages
= 0;
249 MmStats
.NrSystemPages
= 0;
250 MmStats
.NrUserPages
= 0;
251 MmStats
.NrReservedPages
= 0;
252 MmStats
.NrUserPages
= 0;
253 MmStats
.NrFreePages
= 0;
254 MmStats
.NrLockedPages
= 0;
255 MmStats
.PagingRequestsInLastMinute
= 0;
256 MmStats
.PagingRequestsInLastFiveMinutes
= 0;
257 MmStats
.PagingRequestsInLastFifteenMinutes
= 0;
260 * Initialize the kernel address space
262 MmInitializeKernelAddressSpace();
268 /* FIXME: This is broken in SMP mode */
269 //MmDeletePageTable(NULL, 0);
272 * Free all pages not used for kernel memory
273 * (we assume the kernel occupies a continuous range of physical
276 DPRINT("first krnl %x\nlast krnl %x\n",FirstKrnlPhysAddr
,
280 * Free physical memory not used by the kernel
282 MmStats
.NrTotalPages
= KeLoaderBlock
.MemHigher
/4;
283 if (!MmStats
.NrTotalPages
)
285 DbgPrint("Memory not detected, default to 8 MB\n");
286 MmStats
.NrTotalPages
= 2048;
290 /* add 1MB for standard memory (not extended) */
291 MmStats
.NrTotalPages
+= 256;
294 MmStats
.NrTotalPages
+= 16;
296 DbgPrint("Used memory %dKb\n", (MmStats
.NrTotalPages
* PAGESIZE
) / 1024);
298 LastKernelAddress
= (ULONG
)MmInitializePageList(
299 (PVOID
)FirstKrnlPhysAddr
,
300 (PVOID
)LastKrnlPhysAddr
,
301 MmStats
.NrTotalPages
,
302 PAGE_ROUND_UP(LastKernelAddress
),
305 kernel_len
= LastKrnlPhysAddr
- FirstKrnlPhysAddr
;
308 * Create a trap for null pointer references and protect text
312 DPRINT("_text_start__ %x _text_end__ %x\n",(int)&_text_start__
,(int)&_text_end__
);
313 for (i
=PAGE_ROUND_UP(((int)&_text_start__
));
314 i
<PAGE_ROUND_DOWN(((int)&_text_end__
));i
=i
+PAGESIZE
)
316 MmSetPageProtect(NULL
,
321 DPRINT("Invalidating between %x and %x\n",
323 KERNEL_BASE
+ PAGE_TABLE_SIZE
);
324 for (i
=(LastKernelAddress
);
325 i
<(KERNEL_BASE
+ PAGE_TABLE_SIZE
);
328 MmDeleteVirtualMapping(NULL
, (PVOID
)(i
), FALSE
, NULL
, NULL
);
330 DPRINT("Almost done MmInit()\n");
332 /* FIXME: This is broken in SMP mode */
333 MmDeleteVirtualMapping(NULL
, (PVOID
)&unmap_me
, FALSE
, NULL
, NULL
);
334 MmDeleteVirtualMapping(NULL
, (PVOID
)&unmap_me2
, FALSE
, NULL
, NULL
);
335 MmDeleteVirtualMapping(NULL
, (PVOID
)&unmap_me3
, FALSE
, NULL
, NULL
);
338 * Intialize memory areas
340 MmInitVirtualMemory(LastKernelAddress
, kernel_len
);
345 MmInitSectionImplementation();
352 MmCreatePhysicalMemorySection();
353 MmInitializeRmapList();
355 /* FIXME: Read parameters from memory */