1 /* $Id: mminit.c,v 1.18 2001/04/13 16:12:26 chorns 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>
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>
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
;
83 DPRINT("MmInitVirtualMemory(%x, %x)\n",LastKernelAddress
, KernelLength
);
85 LastKernelAddress
= PAGE_ROUND_UP(LastKernelAddress
);
88 // ExInitNonPagedPool(KERNEL_BASE + PAGE_ROUND_UP(kernel_len) + PAGESIZE);
89 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
;
98 MmCreateMemoryArea(NULL
,
99 MmGetKernelAddressSpace(),
106 Length
= PAGE_ROUND_UP(((ULONG
)&_bss_end__
)) -
107 PAGE_ROUND_UP(((ULONG
)&_text_end__
));
108 ParamLength
= ParamLength
- Length
;
109 DPRINT("Length %x\n",Length
);
110 BaseAddress
= (PVOID
)PAGE_ROUND_UP(((ULONG
)&_text_end__
));
111 DPRINT("BaseAddress %x\n",BaseAddress
);
112 MmCreateMemoryArea(NULL
,
113 MmGetKernelAddressSpace(),
121 BaseAddress
= (PVOID
)PAGE_ROUND_UP(((ULONG
)&_bss_end__
));
122 // Length = ParamLength;
123 Length
= LastKernelAddress
- (ULONG
)BaseAddress
;
124 MmCreateMemoryArea(NULL
,
125 MmGetKernelAddressSpace(),
133 BaseAddress
= (PVOID
)(LastKernelAddress
+ PAGESIZE
);
134 Length
= NONPAGED_POOL_SIZE
;
135 MmCreateMemoryArea(NULL
,
136 MmGetKernelAddressSpace(),
144 BaseAddress
= (PVOID
)KERNEL_SHARED_DATA_BASE
;
146 MmCreateMemoryArea(NULL
,
147 MmGetKernelAddressSpace(),
152 &kernel_shared_data_desc
,
155 MmSharedDataPagePhysicalAddress
= MmAllocPage(0);
156 Status
= MmCreateVirtualMapping(NULL
,
157 (PVOID
)KERNEL_SHARED_DATA_BASE
,
159 (ULONG
)MmSharedDataPagePhysicalAddress
);
160 if (!NT_SUCCESS(Status
))
162 DbgPrint("Unable to create virtual mapping\n");
165 ((PKUSER_SHARED_DATA
)KERNEL_SHARED_DATA_BASE
)->TickCountLow
= 0xdeadbeef;
167 // MmDumpMemoryAreas();
168 DPRINT("MmInitVirtualMemory() done\n");
171 VOID
MmInit1(ULONG FirstKrnlPhysAddr
,
172 ULONG LastKrnlPhysAddr
,
173 ULONG LastKernelAddress
)
175 * FUNCTION: Initalize memory managment
181 extern unsigned int unmap_me
, unmap_me2
, unmap_me3
;
184 DPRINT("MmInit1(FirstKrnlPhysAddr, %x, LastKrnlPhysAddr %x, LastKernelAddress %x)\n",
190 * FIXME: Set this based on the system command line
192 MmUserProbeAddress
= (PVOID
)0x7fff0000;
193 MmHighestUserAddress
= (PVOID
)0x7ffeffff;
196 * Initialize memory managment statistics
198 MmStats
.NrTotalPages
= 0;
199 MmStats
.NrSystemPages
= 0;
200 MmStats
.NrUserPages
= 0;
201 MmStats
.NrReservedPages
= 0;
202 MmStats
.NrUserPages
= 0;
203 MmStats
.NrFreePages
= 0;
204 MmStats
.NrLockedPages
= 0;
205 MmStats
.PagingRequestsInLastMinute
= 0;
206 MmStats
.PagingRequestsInLastFiveMinutes
= 0;
207 MmStats
.PagingRequestsInLastFifteenMinutes
= 0;
210 * Initialize the kernel address space
212 MmInitializeKernelAddressSpace();
218 /* FIXME: This is broken in SMP mode */
219 MmDeletePageTable(NULL
, 0);
222 * Free all pages not used for kernel memory
223 * (we assume the kernel occupies a continuous range of physical
226 DPRINT("first krnl %x\nlast krnl %x\n",FirstKrnlPhysAddr
,
230 * Free physical memory not used by the kernel
232 MmStats
.NrTotalPages
= KeLoaderBlock
.MemHigher
/4;
233 if (!MmStats
.NrTotalPages
)
235 DbgPrint("Memory not detected, default to 8 MB\n");
236 MmStats
.NrTotalPages
= 2048;
240 /* add 1MB for standard memory (not extended) */
241 MmStats
.NrTotalPages
+= 256;
243 DbgPrint("Used memory %dKb\n", (MmStats
.NrTotalPages
* PAGESIZE
) / 1024);
245 LastKernelAddress
= (ULONG
)MmInitializePageList(
246 (PVOID
)FirstKrnlPhysAddr
,
247 (PVOID
)LastKrnlPhysAddr
,
248 MmStats
.NrTotalPages
,
249 PAGE_ROUND_UP(LastKernelAddress
));
250 kernel_len
= LastKrnlPhysAddr
- FirstKrnlPhysAddr
;
253 * Create a trap for null pointer references and protect text
257 DPRINT("_text_start__ %x _text_end__ %x\n",(int)&_text_start__
,(int)&_text_end__
);
258 for (i
=PAGE_ROUND_UP(((int)&_text_start__
));
259 i
<PAGE_ROUND_DOWN(((int)&_text_end__
));i
=i
+PAGESIZE
)
261 MmSetPageProtect(NULL
,
266 DPRINT("Invalidating between %x and %x\n",
268 KERNEL_BASE
+ PAGE_TABLE_SIZE
);
269 for (i
=(LastKernelAddress
);
270 i
<(KERNEL_BASE
+ PAGE_TABLE_SIZE
);
273 MmDeleteVirtualMapping(NULL
, (PVOID
)(i
), FALSE
, NULL
, NULL
);
275 DPRINT("Almost done MmInit()\n");
277 /* FIXME: This is broken in SMP mode */
278 MmDeleteVirtualMapping(NULL
, (PVOID
)&unmap_me
, FALSE
, NULL
, NULL
);
279 MmDeleteVirtualMapping(NULL
, (PVOID
)&unmap_me2
, FALSE
, NULL
, NULL
);
280 MmDeleteVirtualMapping(NULL
, (PVOID
)&unmap_me3
, FALSE
, NULL
, NULL
);
283 * Intialize memory areas
285 MmInitVirtualMemory(LastKernelAddress
, kernel_len
);
290 MmInitSectionImplementation();
297 MmCreatePhysicalMemorySection();
299 /* FIXME: Read parameters from memory */