Replaced linux io calls
[reactos.git] / reactos / ntoskrnl / mm / mminit.c
1 /* $Id: mminit.c,v 1.9 2000/10/22 13:28:20 ekohl Exp $
2 *
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)
8 * UPDATE HISTORY:
9 * Created 9/4/98
10 */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <ddk/ntddk.h>
15 #include <internal/i386/segment.h>
16 #include <internal/stddef.h>
17 #include <internal/mm.h>
18 #include <string.h>
19 #include <internal/string.h>
20 #include <internal/ntoskrnl.h>
21 #include <internal/bitops.h>
22 #include <internal/string.h>
23 #include <internal/io.h>
24 #include <internal/ps.h>
25 #include <internal/mmhal.h>
26 #include <napi/shared_data.h>
27
28 #define NDEBUG
29 #include <internal/debug.h>
30
31 /* GLOBALS *****************************************************************/
32
33 /*
34 * Size of extended memory (kb) (fixed for now)
35 */
36 #define EXTENDED_MEMORY_SIZE (3*1024*1024)
37
38 /*
39 * Compiler defined symbol s
40 */
41 extern unsigned int _text_start__;
42 extern unsigned int _text_end__;
43
44 static BOOLEAN IsThisAnNtAsSystem = FALSE;
45 static MM_SYSTEM_SIZE MmSystemSize = MmSmallSystem;
46
47 extern unsigned int _bss_end__;
48
49 static MEMORY_AREA* kernel_text_desc = NULL;
50 static MEMORY_AREA* kernel_data_desc = NULL;
51 static MEMORY_AREA* kernel_param_desc = NULL;
52 static MEMORY_AREA* kernel_pool_desc = NULL;
53 static MEMORY_AREA* kernel_shared_data_desc = NULL;
54
55 PVOID MmSharedDataPagePhysicalAddress = NULL;
56
57 /* FUNCTIONS ****************************************************************/
58
59 BOOLEAN STDCALL MmIsThisAnNtAsSystem(VOID)
60 {
61 return(IsThisAnNtAsSystem);
62 }
63
64 MM_SYSTEM_SIZE STDCALL MmQuerySystemSize(VOID)
65 {
66 return(MmSystemSize);
67 }
68
69 VOID MiShutdownMemoryManager(VOID)
70 {
71 }
72
73 VOID MmInitVirtualMemory(ULONG LastKernelAddress,
74 ULONG KernelLength)
75 /*
76 * FUNCTION: Intialize the memory areas list
77 * ARGUMENTS:
78 * bp = Pointer to the boot parameters
79 * kernel_len = Length of the kernel
80 */
81 {
82 PVOID BaseAddress;
83 ULONG Length;
84 ULONG ParamLength = KernelLength;
85 NTSTATUS Status;
86
87 DPRINT("MmInitVirtualMemory(%x)\n",bp);
88
89 LastKernelAddress = PAGE_ROUND_UP(LastKernelAddress);
90
91 MmInitMemoryAreas();
92 // ExInitNonPagedPool(KERNEL_BASE + PAGE_ROUND_UP(kernel_len) + PAGESIZE);
93 ExInitNonPagedPool(LastKernelAddress + PAGESIZE);
94
95
96 /*
97 * Setup the system area descriptor list
98 */
99 BaseAddress = (PVOID)KERNEL_BASE;
100 Length = PAGE_ROUND_UP(((ULONG)&_text_end__)) - KERNEL_BASE;
101 ParamLength = ParamLength - Length;
102 MmCreateMemoryArea(NULL,
103 MmGetKernelAddressSpace(),
104 MEMORY_AREA_SYSTEM,
105 &BaseAddress,
106 Length,
107 0,
108 &kernel_text_desc);
109
110 Length = PAGE_ROUND_UP(((ULONG)&_bss_end__)) -
111 PAGE_ROUND_UP(((ULONG)&_text_end__));
112 ParamLength = ParamLength - Length;
113 DPRINT("Length %x\n",Length);
114 BaseAddress = (PVOID)PAGE_ROUND_UP(((ULONG)&_text_end__));
115 DPRINT("BaseAddress %x\n",BaseAddress);
116 MmCreateMemoryArea(NULL,
117 MmGetKernelAddressSpace(),
118 MEMORY_AREA_SYSTEM,
119 &BaseAddress,
120 Length,
121 0,
122 &kernel_data_desc);
123
124 BaseAddress = (PVOID)PAGE_ROUND_UP(((ULONG)&_bss_end__));
125 // Length = ParamLength;
126 Length = LastKernelAddress - (ULONG)BaseAddress;
127 MmCreateMemoryArea(NULL,
128 MmGetKernelAddressSpace(),
129 MEMORY_AREA_SYSTEM,
130 &BaseAddress,
131 Length,
132 0,
133 &kernel_param_desc);
134
135 BaseAddress = (PVOID)(LastKernelAddress + PAGESIZE);
136 Length = NONPAGED_POOL_SIZE;
137 MmCreateMemoryArea(NULL,
138 MmGetKernelAddressSpace(),
139 MEMORY_AREA_SYSTEM,
140 &BaseAddress,
141 Length,
142 0,
143 &kernel_pool_desc);
144
145 BaseAddress = (PVOID)KERNEL_SHARED_DATA_BASE;
146 Length = PAGESIZE;
147 MmCreateMemoryArea(NULL,
148 MmGetKernelAddressSpace(),
149 MEMORY_AREA_SYSTEM,
150 &BaseAddress,
151 Length,
152 0,
153 &kernel_shared_data_desc);
154 MmSharedDataPagePhysicalAddress = MmAllocPage(0);
155 Status = MmCreateVirtualMapping(NULL,
156 (PVOID)KERNEL_SHARED_DATA_BASE,
157 PAGE_READWRITE,
158 (ULONG)MmSharedDataPagePhysicalAddress);
159 if (!NT_SUCCESS(Status))
160 {
161 DbgPrint("Unable to create virtual mapping\n");
162 KeBugCheck(0);
163 }
164 ((PKUSER_SHARED_DATA)KERNEL_SHARED_DATA_BASE)->TickCountLow = 0xdeadbeef;
165
166 // MmDumpMemoryAreas();
167 DPRINT("MmInitVirtualMemory() done\n");
168 }
169
170 VOID MmInit1(ULONG FirstKrnlPhysAddr,
171 ULONG LastKrnlPhysAddr,
172 ULONG LastKernelAddress)
173 /*
174 * FUNCTION: Initalize memory managment
175 */
176 {
177 ULONG i;
178 ULONG kernel_len;
179
180 DPRINT("MmInit1(bp %x, LastKernelAddress %x)\n", bp,
181 LastKernelAddress);
182
183 /*
184 * FIXME: Set this based on the system command line
185 */
186 MmUserProbeAddress = (PVOID)0x7fff0000;
187 MmHighestUserAddress = (PVOID)0x7ffeffff;
188
189 /*
190 * Initialize memory managment statistics
191 */
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;
202
203 /*
204 * Initialize the kernel address space
205 */
206 MmInitializeKernelAddressSpace();
207
208 /*
209 * Unmap low memory
210 */
211 MmDeletePageTable(NULL, 0);
212
213 /*
214 * Free all pages not used for kernel memory
215 * (we assume the kernel occupies a continuous range of physical
216 * memory)
217 */
218 DPRINT("first krnl %x\nlast krnl %x\n",FirstKrnlPhysAddr,
219 LastKrnlPhysAddr);
220
221 /*
222 * Free physical memory not used by the kernel
223 */
224 LastKernelAddress = (ULONG)MmInitializePageList(
225 (PVOID)FirstKrnlPhysAddr,
226 (PVOID)LastKrnlPhysAddr,
227 1024,
228 PAGE_ROUND_UP(LastKernelAddress));
229 kernel_len = LastKrnlPhysAddr - FirstKrnlPhysAddr;
230
231 /*
232 * Create a trap for null pointer references and protect text
233 * segment
234 */
235 CHECKPOINT;
236 DPRINT("stext %x etext %x\n",(int)&stext,(int)&etext);
237 for (i=PAGE_ROUND_UP(((int)&_text_start__));
238 i<PAGE_ROUND_DOWN(((int)&_text_end__));i=i+PAGESIZE)
239 {
240 MmSetPageProtect(NULL,
241 (PVOID)i,
242 PAGE_EXECUTE_READ);
243 }
244
245 DPRINT("Invalidating between %x and %x\n",
246 LastKernelAddress,
247 KERNEL_BASE + PAGE_TABLE_SIZE);
248 for (i=(LastKernelAddress);
249 i<(KERNEL_BASE + PAGE_TABLE_SIZE);
250 i=i+PAGESIZE)
251 {
252 MmDeleteVirtualMapping(NULL, (PVOID)(i), FALSE);
253 }
254 DPRINT("Almost done MmInit()\n");
255
256 /*
257 * Intialize memory areas
258 */
259 MmInitVirtualMemory(LastKernelAddress, kernel_len);
260 }
261
262 VOID MmInit2(VOID)
263 {
264 MmInitSectionImplementation();
265 MmInitPagingFile();
266 }
267
268 VOID MmInit3(VOID)
269 {
270 MmInitPagerThread();
271 /* FIXME: Read parameters from memory */
272 }
273