Added working set functions
[reactos.git] / reactos / ntoskrnl / mm / mminit.c
1 /* $Id: mminit.c,v 1.1 2000/07/04 08:52:42 dwelch 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/hal/io.h>
16 #include <internal/i386/segment.h>
17 #include <internal/stddef.h>
18 #include <internal/mm.h>
19 #include <string.h>
20 #include <internal/string.h>
21 #include <internal/ntoskrnl.h>
22 #include <internal/bitops.h>
23 #include <internal/string.h>
24 #include <internal/io.h>
25 #include <internal/ps.h>
26 #include <internal/mmhal.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 stext;
42 extern unsigned int etext;
43 extern unsigned int end;
44
45 static BOOLEAN IsThisAnNtAsSystem = FALSE;
46 static MM_SYSTEM_SIZE MmSystemSize = MmSmallSystem;
47
48 extern unsigned int etext;
49 extern unsigned int _bss_end__;
50
51 static MEMORY_AREA* kernel_text_desc = NULL;
52 static MEMORY_AREA* kernel_data_desc = NULL;
53 static MEMORY_AREA* kernel_param_desc = NULL;
54 static MEMORY_AREA* kernel_pool_desc = NULL;
55
56 /* FUNCTIONS ****************************************************************/
57
58 BOOLEAN STDCALL MmIsThisAnNtAsSystem(VOID)
59 {
60 return(IsThisAnNtAsSystem);
61 }
62
63 MM_SYSTEM_SIZE STDCALL MmQuerySystemSize(VOID)
64 {
65 return(MmSystemSize);
66 }
67
68 VOID MiShutdownMemoryManager(VOID)
69 {
70 }
71
72 VOID MmInitVirtualMemory(boot_param* bp, ULONG LastKernelAddress)
73 /*
74 * FUNCTION: Intialize the memory areas list
75 * ARGUMENTS:
76 * bp = Pointer to the boot parameters
77 * kernel_len = Length of the kernel
78 */
79 {
80 unsigned int kernel_len = bp->end_mem - bp->start_mem;
81 PVOID BaseAddress;
82 ULONG Length;
83 ULONG ParamLength = kernel_len;
84
85 DPRINT("MmInitVirtualMemory(%x)\n",bp);
86
87 LastKernelAddress = PAGE_ROUND_UP(LastKernelAddress);
88
89 MmInitMemoryAreas();
90 // ExInitNonPagedPool(KERNEL_BASE + PAGE_ROUND_UP(kernel_len) + PAGESIZE);
91 ExInitNonPagedPool(LastKernelAddress + PAGESIZE);
92
93
94 /*
95 * Setup the system area descriptor list
96 */
97 BaseAddress = (PVOID)KERNEL_BASE;
98 Length = PAGE_ROUND_UP(((ULONG)&etext)) - KERNEL_BASE;
99 ParamLength = ParamLength - Length;
100 MmCreateMemoryArea(NULL,
101 MmGetKernelAddressSpace(),
102 MEMORY_AREA_SYSTEM,
103 &BaseAddress,
104 Length,
105 0,
106 &kernel_text_desc);
107
108 Length = PAGE_ROUND_UP(((ULONG)&_bss_end__)) -
109 PAGE_ROUND_UP(((ULONG)&etext));
110 ParamLength = ParamLength - Length;
111 DPRINT("Length %x\n",Length);
112 BaseAddress = (PVOID)PAGE_ROUND_UP(((ULONG)&etext));
113 DPRINT("BaseAddress %x\n",BaseAddress);
114 MmCreateMemoryArea(NULL,
115 MmGetKernelAddressSpace(),
116 MEMORY_AREA_SYSTEM,
117 &BaseAddress,
118 Length,
119 0,
120 &kernel_data_desc);
121
122 BaseAddress = (PVOID)PAGE_ROUND_UP(((ULONG)&end));
123 // Length = ParamLength;
124 Length = LastKernelAddress - (ULONG)BaseAddress;
125 MmCreateMemoryArea(NULL,
126 MmGetKernelAddressSpace(),
127 MEMORY_AREA_SYSTEM,
128 &BaseAddress,
129 Length,
130 0,
131 &kernel_param_desc);
132
133 BaseAddress = (PVOID)(LastKernelAddress + PAGESIZE);
134 Length = NONPAGED_POOL_SIZE;
135 MmCreateMemoryArea(NULL,
136 MmGetKernelAddressSpace(),
137 MEMORY_AREA_SYSTEM,
138 &BaseAddress,
139 Length,
140 0,
141 &kernel_pool_desc);
142
143 // MmDumpMemoryAreas();
144 DPRINT("MmInitVirtualMemory() done\n");
145 }
146
147 VOID MmInit1(PLOADER_PARAMETER_BLOCK bp, ULONG LastKernelAddress)
148 /*
149 * FUNCTION: Initalize memory managment
150 */
151 {
152 ULONG first_krnl_phys_addr;
153 ULONG last_krnl_phys_addr;
154 ULONG i;
155 ULONG kernel_len;
156
157 DPRINT("MmInit1(bp %x, LastKernelAddress %x)\n", bp,
158 LastKernelAddress);
159
160 MmInitializeKernelAddressSpace();
161
162 /*
163 * Unmap low memory
164 */
165 MmDeletePageTable(NULL, 0);
166
167 /*
168 * Free all pages not used for kernel memory
169 * (we assume the kernel occupies a continuous range of physical
170 * memory)
171 */
172 first_krnl_phys_addr = bp->start_mem;
173 last_krnl_phys_addr = bp->end_mem;
174 DPRINT("first krnl %x\nlast krnl %x\n",first_krnl_phys_addr,
175 last_krnl_phys_addr);
176
177 /*
178 * Free physical memory not used by the kernel
179 */
180 LastKernelAddress = (ULONG)MmInitializePageList(
181 (PVOID)first_krnl_phys_addr,
182 (PVOID)last_krnl_phys_addr,
183 1024,
184 PAGE_ROUND_UP(LastKernelAddress));
185 kernel_len = last_krnl_phys_addr - first_krnl_phys_addr;
186
187 /*
188 * Create a trap for null pointer references and protect text
189 * segment
190 */
191 CHECKPOINT;
192 DPRINT("stext %x etext %x\n",(int)&stext,(int)&etext);
193 for (i=PAGE_ROUND_UP(((int)&stext));
194 i<PAGE_ROUND_DOWN(((int)&etext));i=i+PAGESIZE)
195 {
196 MmSetPageProtect(NULL,
197 (PVOID)i,
198 PAGE_EXECUTE_READ);
199 }
200
201 DPRINT("Invalidating between %x and %x\n",
202 LastKernelAddress,
203 KERNEL_BASE + PAGE_TABLE_SIZE);
204 for (i=(LastKernelAddress);
205 i<(KERNEL_BASE + PAGE_TABLE_SIZE);
206 i=i+PAGESIZE)
207 {
208 MmSetPage(NULL, (PVOID)(i), PAGE_NOACCESS, 0);
209 }
210 DPRINT("Almost done MmInit()\n");
211
212 /*
213 * Intialize memory areas
214 */
215 MmInitVirtualMemory(bp, LastKernelAddress);
216 }
217
218 VOID MmInit2(VOID)
219 {
220 MmInitSectionImplementation();
221 MmInitPagingFile();
222 }
223
224 VOID MmInit3(VOID)
225 {
226 MmInitPagerThread();
227 /* FIXME: Read parameters from memory */
228 }
229