ReactOS 0.4.16-dev-250-g3ecd236
mminit.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/mm/mminit.c
5 * PURPOSE: Memory Manager Initialization
6 * PROGRAMMERS:
7 */
8
9/* INCLUDES ******************************************************************/
10
11#include <ntoskrnl.h>
12#define NDEBUG
13#include <debug.h>
14
15#define MODULE_INVOLVED_IN_ARM3
16#include "ARM3/miarm.h"
17
18/* GLOBALS *******************************************************************/
19
22//
23// 0 | 1 is on/off paging, 2 is undocumented
24//
25UCHAR MmDisablePagingExecutive = 1; // Forced to off
28
33
34/* PRIVATE FUNCTIONS *********************************************************/
35
36//
37// Helper function to create initial memory areas.
38// The created area is always read/write.
39//
40CODE_SEG("INIT")
41VOID
44{
45 const ULONG Protection = Executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
46 PVOID pBaseAddress = BaseAddress;
47 PMEMORY_AREA MArea;
49
52 &pBaseAddress,
53 Size,
54 Protection,
55 &MArea,
56 0,
57 PAGE_SIZE);
59 // TODO: Perhaps it would be prudent to bugcheck here, not only assert?
60}
61
62CODE_SEG("INIT")
63VOID
66{
67 //
68 // Create all the static memory areas.
69 //
70
72
73#ifdef _M_AMD64
74 // Reserved range FFFF800000000000 - FFFFF68000000000
76#endif /* _M_AMD64 */
77
78 // The loader mappings. The only Executable area.
80
81 // The PTE base
83
84 // Hyperspace
86
87 // Protect the PFN database
89
90 // ReactOS requires a memory area to keep the initial NP area off-bounds
92
93 // System PTE space
95
96 // Nonpaged pool expansion space
98
99 // System view space
101
102 // Session space
104
105 // Paged pool
107
108 // Debugger mapping
110
111#if defined(_X86_)
112 // Reserved HAL area (includes KUSER_SHARED_DATA and KPCR)
114#else /* _X86_ */
115#ifndef _M_AMD64
116 // KPCR, one page per CPU. Only for 32-bit kernel.
118#endif /* _M_AMD64 */
119
120 // KUSER_SHARED_DATA
122#endif /* _X86_ */
123
125}
126
127CODE_SEG("INIT")
128VOID
129NTAPI
131{
132 //
133 // Print the memory layout
134 //
135 DPRINT1(" 0x%p - 0x%p\t%s\n",
138 "Boot Loaded Image");
139 DPRINT1(" 0x%p - 0x%p\t%s\n",
142 "PFN Database");
143 DPRINT1(" 0x%p - 0x%p\t%s\n",
146 "ARM3 Non Paged Pool");
147 DPRINT1(" 0x%p - 0x%p\t%s\n",
150 "System View Space");
151 DPRINT1(" 0x%p - 0x%p\t%s\n",
154 "Session Space");
155 DPRINT1(" 0x%p - 0x%p\t%s\n",
157 "Page Tables");
158 DPRINT1(" 0x%p - 0x%p\t%s\n",
160 "Page Directories");
161 DPRINT1(" 0x%p - 0x%p\t%s\n",
163 "Hyperspace");
164 DPRINT1(" 0x%p - 0x%p\t%s\n",
166 "System Cache");
167 DPRINT1(" 0x%p - 0x%p\t%s\n",
170 "ARM3 Paged Pool");
171 DPRINT1(" 0x%p - 0x%p\t%s\n",
173 "System PTE Space");
174 DPRINT1(" 0x%p - 0x%p\t%s\n",
176 "Non Paged Pool Expansion PTE Space");
177}
178
179CODE_SEG("INIT")
181NTAPI
183{
186 HANDLE ThreadHandle;
187
188 /* Create the thread */
190 Status = PsCreateSystemThread(&ThreadHandle,
193 NULL,
194 NULL,
196 NULL);
197
198 /* Close the handle and return status */
199 ZwClose(ThreadHandle);
200 return Status;
201}
202
203CODE_SEG("INIT")
205NTAPI
207 IN PLOADER_PARAMETER_BLOCK LoaderBlock)
208{
209 extern MMPTE ValidKernelPte;
210 PMMPTE PointerPte;
212 PFN_NUMBER PageFrameNumber;
213 PLIST_ENTRY ListEntry;
214 PLDR_DATA_TABLE_ENTRY DataTableEntry;
215
216 /* Initialize the kernel address space */
217 ASSERT(Phase == 1);
218
219#ifdef NEWCC
223 // Until we're fully demand paged, we can do things the old way through
224 // the balance manager
225 // CcInitView will override this...
227#else
229#endif
230
232
233 /* Intialize system memory areas */
235
236 /* Dump the address space */
238
244
245 //
246 // Create a PTE to double-map the shared data section. We allocate it
247 // from paged pool so that we can't fault when trying to touch the PTE
248 // itself (to map it), since paged pool addresses will already be mapped
249 // by the fault handler.
250 //
252 sizeof(MMPTE),
253 TAG_MM);
254 if (!MmSharedUserDataPte) return FALSE;
255
256 //
257 // Now get the PTE for shared data, and read the PFN that holds it
258 //
260 ASSERT(PointerPte->u.Hard.Valid == 1);
261 PageFrameNumber = PFN_FROM_PTE(PointerPte);
262
263 /* Build the PTE and write it */
265 PointerPte,
267 PageFrameNumber);
269
270 /* Initialize session working set support */
272
273 /* Setup session IDs */
275
276 /* Setup the memory threshold events */
277 if (!MiInitializeMemoryEvents()) return FALSE;
278
279 /*
280 * Unmap low memory
281 */
283
284 /* Initialize the balance set manager */
286
287 /* Loop the boot loaded images (under lock) */
289 for (ListEntry = PsLoadedModuleList.Flink;
290 ListEntry != &PsLoadedModuleList;
291 ListEntry = ListEntry->Flink)
292 {
293 /* Get the data table entry */
294 DataTableEntry = CONTAINING_RECORD(ListEntry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);
295
296 /* Set up the image protection */
297 MiWriteProtectSystemImage(DataTableEntry->DllBase);
298 }
300
301 return TRUE;
302}
303
PVOID MmNonPagedSystemStart
Definition: mminit.c:96
PVOID MmPagedPoolStart
Definition: mminit.c:104
SIZE_T MmBootImageSize
Definition: mminit.c:222
PVOID MmNonPagedPoolEnd
Definition: mminit.c:99
BOOLEAN NTAPI MiInitializeMemoryEvents(VOID)
Definition: mminit.c:1339
SIZE_T MmSizeOfNonPagedPoolInBytes
Definition: mminit.c:26
PVOID MmNonPagedPoolExpansionStart
Definition: mminit.c:98
PVOID MmSessionBase
Definition: mminit.c:134
PVOID MiSystemViewStart
Definition: mminit.c:154
PFN_NUMBER MxPfnAllocation
Definition: mminit.c:187
PVOID MmNonPagedPoolStart
Definition: mminit.c:97
PFN_COUNT MmNumberOfSystemPtes
Definition: mminit.c:181
SIZE_T MmSystemViewSize
Definition: mminit.c:155
PVOID MiSessionSpaceEnd
Definition: mminit.c:127
SIZE_T MmSizeOfPagedPoolInBytes
Definition: mminit.c:110
PVOID MmSystemCacheStart
Definition: mminit.c:243
PVOID MmSystemCacheEnd
Definition: mminit.c:244
#define CODE_SEG(...)
unsigned char BOOLEAN
HARDWARE_PTE_ARMV6 TempPte
Definition: winldr.c:76
#define PDE_BASE
Definition: winldr.c:21
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
#define MM_READONLY
Definition: bootanim.c:18
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
#define ExAcquireResourceExclusiveLite(res, wait)
Definition: env_spec_w32.h:615
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
#define PagedPool
Definition: env_spec_w32.h:308
Status
Definition: gdiplustypes.h:25
CCHAR KeNumberProcessors
Definition: krnlinit.c:35
VOID NTAPI MiInitializeSessionIds(VOID)
Definition: session.c:116
VOID NTAPI MiWriteProtectSystemImage(_In_ PVOID ImageBase)
Definition: sysldr.c:2481
FORCEINLINE VOID MI_MAKE_HARDWARE_PTE_KERNEL(IN PMMPTE NewPte, IN PMMPTE MappingPte, IN ULONG_PTR ProtectionMask, IN PFN_NUMBER PageFrameNumber)
Definition: miarm.h:781
VOID NTAPI MiInitializeSessionWsSupport(VOID)
Definition: session.c:40
PMMSUPPORT MmKernelAddressSpace
Definition: mminit.c:27
FAST_MUTEX MiGlobalPageOperation
Definition: swapout.c:74
NTSTATUS MiRosTrimCache(ULONG Target, ULONG Priority, PULONG NrFreed)
Definition: swapout.c:633
BOOLEAN Mm64BitPhysicalAddress
Definition: mminit.c:20
PMMPTE MmSharedUserDataPte
Definition: mminit.c:26
VOID NTAPI MiInitSystemMemoryAreas(VOID)
Definition: mminit.c:65
NTSTATUS NTAPI MmInitBsmThread(VOID)
Definition: mminit.c:182
VOID NTAPI MiDbgDumpAddressSpace(VOID)
Definition: mminit.c:130
VOID NTAPI MiCreateArm3StaticMemoryArea(PVOID BaseAddress, SIZE_T Size, BOOLEAN Executable)
Definition: mminit.c:43
UCHAR MmDisablePagingExecutive
Definition: mminit.c:25
ULONG MmReadClusterSize
Definition: mminit.c:21
LIST_ENTRY MiSegmentList
Definition: data.c:86
KEVENT MmWaitPageEvent
Definition: fault.c:461
BOOLEAN NTAPI MmInitSystem(IN ULONG Phase, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: mminit.c:206
#define MiAddressToPte(x)
Definition: mmx86.c:19
#define PTE_BASE
Definition: mmx86.c:14
#define ASSERT(a)
Definition: mode.c:44
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define KSEG0_BASE
Definition: ketypes.h:343
#define MM_HAL_VA_END
Definition: ketypes.h:327
#define MM_HAL_VA_START
Definition: ketypes.h:326
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
#define THREAD_ALL_ACCESS
Definition: nt_native.h:1339
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
@ SynchronizationEvent
VOID FASTCALL ExReleaseResourceLite(IN PERESOURCE Resource)
Definition: resource.c:1822
#define HYPER_SPACE
Definition: mm.h:14
#define MI_REAL_SYSTEM_RANGE_START
Definition: mm.h:12
#define MI_DEBUG_MAPPING
Definition: mm.h:20
#define HYPER_SPACE_END
Definition: mm.h:15
#define PFN_FROM_PTE(v)
Definition: mm.h:92
#define PCR
Definition: ke.h:8
#define PTE_TOP
Definition: mm.h:161
#define PDE_TOP
Definition: mm.h:168
VOID NTAPI KeBalanceSetManager(IN PVOID Context)
Definition: balmgr.c:137
FORCEINLINE VOID MmLockAddressSpace(PMMSUPPORT AddressSpace)
Definition: mm.h:1691
NTSTATUS NTAPI MmCreateMemoryArea(PMMSUPPORT AddressSpace, ULONG Type, PVOID *BaseAddress, SIZE_T Length, ULONG Protection, PMEMORY_AREA *Result, ULONG AllocationFlags, ULONG AllocationGranularity)
Definition: marea.c:410
#define MC_USER
Definition: mm.h:114
PMMPFN MmPfnDatabase
Definition: freelist.c:24
#define MEMORY_AREA_STATIC
Definition: mm.h:98
NTSTATUS MmTrimUserMemory(ULONG Target, ULONG Priority, PULONG NrFreedPages)
Definition: balance.c:138
#define MEMORY_AREA_OWNED_BY_ARM3
Definition: mm.h:97
VOID NTAPI MiInitBalancerThread(VOID)
Definition: balance.c:420
FORCEINLINE VOID MmUnlockAddressSpace(PMMSUPPORT AddressSpace)
Definition: mm.h:1704
VOID NTAPI MmInitializeMemoryConsumer(ULONG Consumer, NTSTATUS(*Trim)(ULONG Target, ULONG Priority, PULONG NrFreed))
Definition: balance.c:57
NTSTATUS NTAPI MmInitSectionImplementation(VOID)
Definition: section.c:2299
VOID NTAPI MmInitializeRmapList(VOID)
Definition: rmap.c:38
FORCEINLINE PMMSUPPORT MmGetKernelAddressSpace(VOID)
Definition: mm.h:1726
VOID NTAPI MmInitGlobalKernelPageDirectory(VOID)
Definition: page.c:277
VOID NTAPI MmInitPagingFile(VOID)
Definition: pagefile.c:272
MMPTE ValidKernelPte
Definition: init.c:29
NTSTATUS NTAPI PsCreateSystemThread(OUT PHANDLE ThreadHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, IN HANDLE ProcessHandle, IN PCLIENT_ID ClientId, IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext)
Definition: thread.c:602
LIST_ENTRY PsLoadedModuleList
Definition: sysldr.c:21
ERESOURCE PsLoadedModuleResource
Definition: sysldr.c:24
PEPROCESS PsIdleProcess
Definition: psmgr.c:51
#define KI_USER_SHARED_DATA
ULONG PFN_NUMBER
Definition: ke.h:9
#define STATUS_SUCCESS
Definition: shellext.h:65
MMSUPPORT Vm
Definition: pstypes.h:1357
Definition: btrfs_drv.h:1876
PVOID DllBase
Definition: btrfs_drv.h:1880
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
ULONG64 Valid
Definition: mmtypes.h:150
union _MMPTE::@2337 u
MMPTE_HARDWARE Hard
Definition: mmtypes.h:217
#define TAG_MM
Definition: tag.h:113
uint32_t * PULONG
Definition: typedefs.h:59
#define NTAPI
Definition: typedefs.h:36
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define IN
Definition: typedefs.h:39
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
uint32_t ULONG
Definition: typedefs.h:59
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
_In_ WDFINTERRUPT _In_ WDF_INTERRUPT_POLICY _In_ WDF_INTERRUPT_PRIORITY Priority
Definition: wdfinterrupt.h:655
_In_ WDFIOTARGET Target
Definition: wdfrequest.h:306
FORCEINLINE VOID ExInitializeFastMutex(_Out_ PFAST_MUTEX FastMutex)
Definition: exfuncs.h:274
FAST_MUTEX
Definition: extypes.h:17
unsigned char UCHAR
Definition: xmlstorage.h:181