1 /* $Id: mminit.c,v 1.2 2000/07/04 11:11:04 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/hal/io.h>
16 #include <internal/i386/segment.h>
17 #include <internal/stddef.h>
18 #include <internal/mm.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>
29 #include <internal/debug.h>
31 /* GLOBALS *****************************************************************/
34 * Size of extended memory (kb) (fixed for now)
36 #define EXTENDED_MEMORY_SIZE (3*1024*1024)
39 * Compiler defined symbol s
41 extern unsigned int stext
;
42 extern unsigned int etext
;
43 extern unsigned int end
;
45 static BOOLEAN IsThisAnNtAsSystem
= FALSE
;
46 static MM_SYSTEM_SIZE MmSystemSize
= MmSmallSystem
;
48 extern unsigned int etext
;
49 extern unsigned int _bss_end__
;
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
;
56 /* FUNCTIONS ****************************************************************/
58 BOOLEAN STDCALL
MmIsThisAnNtAsSystem(VOID
)
60 return(IsThisAnNtAsSystem
);
63 MM_SYSTEM_SIZE STDCALL
MmQuerySystemSize(VOID
)
68 VOID
MiShutdownMemoryManager(VOID
)
72 VOID
MmInitVirtualMemory(PLOADER_PARAMETER_BLOCK bp
, ULONG LastKernelAddress
)
74 * FUNCTION: Intialize the memory areas list
76 * bp = Pointer to the boot parameters
77 * kernel_len = Length of the kernel
80 unsigned int kernel_len
= bp
->end_mem
- bp
->start_mem
;
83 ULONG ParamLength
= kernel_len
;
85 DPRINT("MmInitVirtualMemory(%x)\n",bp
);
87 LastKernelAddress
= PAGE_ROUND_UP(LastKernelAddress
);
90 // ExInitNonPagedPool(KERNEL_BASE + PAGE_ROUND_UP(kernel_len) + PAGESIZE);
91 ExInitNonPagedPool(LastKernelAddress
+ PAGESIZE
);
95 * Setup the system area descriptor list
97 BaseAddress
= (PVOID
)KERNEL_BASE
;
98 Length
= PAGE_ROUND_UP(((ULONG
)&etext
)) - KERNEL_BASE
;
99 ParamLength
= ParamLength
- Length
;
100 MmCreateMemoryArea(NULL
,
101 MmGetKernelAddressSpace(),
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(),
122 BaseAddress
= (PVOID
)PAGE_ROUND_UP(((ULONG
)&end
));
123 // Length = ParamLength;
124 Length
= LastKernelAddress
- (ULONG
)BaseAddress
;
125 MmCreateMemoryArea(NULL
,
126 MmGetKernelAddressSpace(),
133 BaseAddress
= (PVOID
)(LastKernelAddress
+ PAGESIZE
);
134 Length
= NONPAGED_POOL_SIZE
;
135 MmCreateMemoryArea(NULL
,
136 MmGetKernelAddressSpace(),
143 // MmDumpMemoryAreas();
144 DPRINT("MmInitVirtualMemory() done\n");
147 VOID
MmInit1(PLOADER_PARAMETER_BLOCK bp
, ULONG LastKernelAddress
)
149 * FUNCTION: Initalize memory managment
152 ULONG first_krnl_phys_addr
;
153 ULONG last_krnl_phys_addr
;
157 DPRINT("MmInit1(bp %x, LastKernelAddress %x)\n", bp
,
160 MmInitializeKernelAddressSpace();
165 MmDeletePageTable(NULL
, 0);
168 * Free all pages not used for kernel memory
169 * (we assume the kernel occupies a continuous range of physical
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
);
178 * Free physical memory not used by the kernel
180 LastKernelAddress
= (ULONG
)MmInitializePageList(
181 (PVOID
)first_krnl_phys_addr
,
182 (PVOID
)last_krnl_phys_addr
,
184 PAGE_ROUND_UP(LastKernelAddress
));
185 kernel_len
= last_krnl_phys_addr
- first_krnl_phys_addr
;
188 * Create a trap for null pointer references and protect text
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
)
196 MmSetPageProtect(NULL
,
201 DPRINT("Invalidating between %x and %x\n",
203 KERNEL_BASE
+ PAGE_TABLE_SIZE
);
204 for (i
=(LastKernelAddress
);
205 i
<(KERNEL_BASE
+ PAGE_TABLE_SIZE
);
208 MmSetPage(NULL
, (PVOID
)(i
), PAGE_NOACCESS
, 0);
210 DPRINT("Almost done MmInit()\n");
213 * Intialize memory areas
215 MmInitVirtualMemory(bp
, LastKernelAddress
);
220 MmInitSectionImplementation();
227 /* FIXME: Read parameters from memory */