1 /* $Id: mminit.c,v 1.16 2001/03/26 20:46:53 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>
15 #include <internal/i386/segment.h>
16 #include <internal/mm.h>
17 #include <internal/ntoskrnl.h>
18 #include <internal/bitops.h>
19 #include <internal/io.h>
20 #include <internal/ps.h>
21 #include <napi/shared_data.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 symbol s
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
;
50 PVOID MmSharedDataPagePhysicalAddress
= NULL
;
52 /* FUNCTIONS ****************************************************************/
54 BOOLEAN STDCALL
MmIsThisAnNtAsSystem(VOID
)
56 return(IsThisAnNtAsSystem
);
59 MM_SYSTEM_SIZE STDCALL
MmQuerySystemSize(VOID
)
64 VOID
MiShutdownMemoryManager(VOID
)
68 VOID
MmInitVirtualMemory(ULONG LastKernelAddress
,
71 * FUNCTION: Intialize the memory areas list
73 * bp = Pointer to the boot parameters
74 * kernel_len = Length of the kernel
79 ULONG ParamLength
= KernelLength
;
82 DPRINT("MmInitVirtualMemory(%x)\n",bp
);
84 LastKernelAddress
= PAGE_ROUND_UP(LastKernelAddress
);
87 // ExInitNonPagedPool(KERNEL_BASE + PAGE_ROUND_UP(kernel_len) + PAGESIZE);
88 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
;
97 MmCreateMemoryArea(NULL
,
98 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
,
154 MmSharedDataPagePhysicalAddress
= MmAllocPage(0);
155 Status
= MmCreateVirtualMapping(NULL
,
156 (PVOID
)KERNEL_SHARED_DATA_BASE
,
158 (ULONG
)MmSharedDataPagePhysicalAddress
);
159 if (!NT_SUCCESS(Status
))
161 DbgPrint("Unable to create virtual mapping\n");
164 ((PKUSER_SHARED_DATA
)KERNEL_SHARED_DATA_BASE
)->TickCountLow
= 0xdeadbeef;
166 // MmDumpMemoryAreas();
167 DPRINT("MmInitVirtualMemory() done\n");
170 VOID
MmInit1(ULONG FirstKrnlPhysAddr
,
171 ULONG LastKrnlPhysAddr
,
172 ULONG LastKernelAddress
)
174 * FUNCTION: Initalize memory managment
180 DPRINT("MmInit1(bp %x, LastKernelAddress %x)\n", bp
,
184 * FIXME: Set this based on the system command line
186 MmUserProbeAddress
= (PVOID
)0x7fff0000;
187 MmHighestUserAddress
= (PVOID
)0x7ffeffff;
190 * Initialize memory managment statistics
192 MmStats
.NrTotalPages
= 0;
193 MmStats
.NrSystemPages
= 0;
194 MmStats
.NrUserPages
= 0;
195 MmStats
.NrReservedPages
= 0;
196 MmStats
.NrUserPages
= 0;
197 MmStats
.NrFreePages
= 0;
198 MmStats
.NrLockedPages
= 0;
199 MmStats
.PagingRequestsInLastMinute
= 0;
200 MmStats
.PagingRequestsInLastFiveMinutes
= 0;
201 MmStats
.PagingRequestsInLastFifteenMinutes
= 0;
204 * Initialize the kernel address space
206 MmInitializeKernelAddressSpace();
211 MmDeletePageTable(NULL
, 0);
214 * Free all pages not used for kernel memory
215 * (we assume the kernel occupies a continuous range of physical
218 DPRINT("first krnl %x\nlast krnl %x\n",FirstKrnlPhysAddr
,
222 * Free physical memory not used by the kernel
224 MmStats
.NrTotalPages
= KeLoaderBlock
.MemHigher
/4;
225 if (!MmStats
.NrTotalPages
)
227 DbgPrint("Memory not detected, default to 8 MB\n");
228 MmStats
.NrTotalPages
= 2048;
232 /* add 1MB for standard memory (not extended) */
233 MmStats
.NrTotalPages
+= 256;
235 DbgPrint("Used memory %dKb\n", (MmStats
.NrTotalPages
* PAGESIZE
) / 1024);
237 LastKernelAddress
= (ULONG
)MmInitializePageList(
238 (PVOID
)FirstKrnlPhysAddr
,
239 (PVOID
)LastKrnlPhysAddr
,
240 MmStats
.NrTotalPages
,
241 PAGE_ROUND_UP(LastKernelAddress
));
242 kernel_len
= LastKrnlPhysAddr
- FirstKrnlPhysAddr
;
245 * Create a trap for null pointer references and protect text
249 DPRINT("stext %x etext %x\n",(int)&stext
,(int)&etext
);
250 for (i
=PAGE_ROUND_UP(((int)&_text_start__
));
251 i
<PAGE_ROUND_DOWN(((int)&_text_end__
));i
=i
+PAGESIZE
)
253 MmSetPageProtect(NULL
,
258 DPRINT("Invalidating between %x and %x\n",
260 KERNEL_BASE
+ PAGE_TABLE_SIZE
);
261 for (i
=(LastKernelAddress
);
262 i
<(KERNEL_BASE
+ PAGE_TABLE_SIZE
);
265 MmDeleteVirtualMapping(NULL
, (PVOID
)(i
), FALSE
, NULL
, NULL
);
267 DPRINT("Almost done MmInit()\n");
270 * Intialize memory areas
272 MmInitVirtualMemory(LastKernelAddress
, kernel_len
);
277 MmInitSectionImplementation();
284 MmCreatePhysicalMemorySection();
286 /* FIXME: Read parameters from memory */