1 /* $Id: mminit.c,v 1.12 2000/12/28 03:38:07 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 <internal/mmhal.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 symbol s
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)\n",bp
);
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(),
120 BaseAddress
= (PVOID
)PAGE_ROUND_UP(((ULONG
)&_bss_end__
));
121 // Length = ParamLength;
122 Length
= LastKernelAddress
- (ULONG
)BaseAddress
;
123 MmCreateMemoryArea(NULL
,
124 MmGetKernelAddressSpace(),
131 BaseAddress
= (PVOID
)(LastKernelAddress
+ PAGESIZE
);
132 Length
= NONPAGED_POOL_SIZE
;
133 MmCreateMemoryArea(NULL
,
134 MmGetKernelAddressSpace(),
141 BaseAddress
= (PVOID
)KERNEL_SHARED_DATA_BASE
;
143 MmCreateMemoryArea(NULL
,
144 MmGetKernelAddressSpace(),
149 &kernel_shared_data_desc
);
150 MmSharedDataPagePhysicalAddress
= MmAllocPage(0);
151 Status
= MmCreateVirtualMapping(NULL
,
152 (PVOID
)KERNEL_SHARED_DATA_BASE
,
154 (ULONG
)MmSharedDataPagePhysicalAddress
);
155 if (!NT_SUCCESS(Status
))
157 DbgPrint("Unable to create virtual mapping\n");
160 ((PKUSER_SHARED_DATA
)KERNEL_SHARED_DATA_BASE
)->TickCountLow
= 0xdeadbeef;
162 // MmDumpMemoryAreas();
163 DPRINT("MmInitVirtualMemory() done\n");
166 VOID
MmInit1(ULONG FirstKrnlPhysAddr
,
167 ULONG LastKrnlPhysAddr
,
168 ULONG LastKernelAddress
)
170 * FUNCTION: Initalize memory managment
176 DPRINT("MmInit1(bp %x, LastKernelAddress %x)\n", bp
,
180 * FIXME: Set this based on the system command line
182 MmUserProbeAddress
= (PVOID
)0x7fff0000;
183 MmHighestUserAddress
= (PVOID
)0x7ffeffff;
186 * Initialize memory managment statistics
188 MmStats
.NrTotalPages
= 0;
189 MmStats
.NrSystemPages
= 0;
190 MmStats
.NrUserPages
= 0;
191 MmStats
.NrReservedPages
= 0;
192 MmStats
.NrUserPages
= 0;
193 MmStats
.NrFreePages
= 0;
194 MmStats
.NrLockedPages
= 0;
195 MmStats
.PagingRequestsInLastMinute
= 0;
196 MmStats
.PagingRequestsInLastFiveMinutes
= 0;
197 MmStats
.PagingRequestsInLastFifteenMinutes
= 0;
200 * Initialize the kernel address space
202 MmInitializeKernelAddressSpace();
207 MmDeletePageTable(NULL
, 0);
210 * Free all pages not used for kernel memory
211 * (we assume the kernel occupies a continuous range of physical
214 DPRINT("first krnl %x\nlast krnl %x\n",FirstKrnlPhysAddr
,
218 * Free physical memory not used by the kernel
220 MmStats
.NrTotalPages
= KeLoaderBlock
.MemLower
/4;
221 if ( !MmStats
.NrTotalPages
)
223 DbgPrint("Memory not detected, default to 8 MB\n");
224 MmStats
.NrTotalPages
= 2048;
227 MmStats
.NrTotalPages
+= 256;// add 1MB for standard memory (not extended)
228 LastKernelAddress
= (ULONG
)MmInitializePageList(
229 (PVOID
)FirstKrnlPhysAddr
,
230 (PVOID
)LastKrnlPhysAddr
,
232 MmStats
.NrTotalPages
,
233 PAGE_ROUND_UP(LastKernelAddress
));
234 kernel_len
= LastKrnlPhysAddr
- FirstKrnlPhysAddr
;
237 * Create a trap for null pointer references and protect text
241 DPRINT("stext %x etext %x\n",(int)&stext
,(int)&etext
);
242 for (i
=PAGE_ROUND_UP(((int)&_text_start__
));
243 i
<PAGE_ROUND_DOWN(((int)&_text_end__
));i
=i
+PAGESIZE
)
245 MmSetPageProtect(NULL
,
250 DPRINT("Invalidating between %x and %x\n",
252 KERNEL_BASE
+ PAGE_TABLE_SIZE
);
253 for (i
=(LastKernelAddress
);
254 i
<(KERNEL_BASE
+ PAGE_TABLE_SIZE
);
257 MmDeleteVirtualMapping(NULL
, (PVOID
)(i
), FALSE
);
259 DPRINT("Almost done MmInit()\n");
262 * Intialize memory areas
264 MmInitVirtualMemory(LastKernelAddress
, kernel_len
);
269 MmInitSectionImplementation();
276 MmCreatePhysicalMemorySection();
278 /* FIXME: Read parameters from memory */