ReactOS  0.4.10-dev-244-gb941574
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 
21 
24 //
25 // 0 | 1 is on/off paging, 2 is undocumented
26 //
27 UCHAR MmDisablePagingExecutive = 1; // Forced to off
30 
31 extern KEVENT MmWaitPageEvent;
35 
36 /* PRIVATE FUNCTIONS *********************************************************/
37 
38 //
39 // Helper function to create initial memory areas.
40 // The created area is always read/write.
41 //
42 VOID
44 NTAPI
46 {
47  const ULONG Protection = Executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
48  PVOID pBaseAddress = BaseAddress;
49  PMEMORY_AREA MArea;
51 
54  &pBaseAddress,
55  Size,
56  Protection,
57  &MArea,
58  0,
59  PAGE_SIZE);
60  ASSERT(Status == STATUS_SUCCESS);
61  // TODO: Perhaps it would be prudent to bugcheck here, not only assert?
62 }
63 
64 VOID
66 NTAPI
68 {
69  //
70  // Create all the static memory areas.
71  //
72 
73  // The loader mappings. The only Executable area.
75 
76  // The PTE base
78 
79  // Hyperspace
81 
82  // Protect the PFN database
84 
85  // ReactOS requires a memory area to keep the initial NP area off-bounds
87 
88  // System PTE space
90 
91  // Nonpaged pool expansion space
93 
94  // System view space
96 
97  // Session space
99 
100  // Paged pool
102 
103  // Debugger mapping
105 
106 #if defined(_X86_)
107  // Reserved HAL area (includes KUSER_SHARED_DATA and KPCR)
109 #else /* _X86_ */
110 #ifndef _M_AMD64
111  // KPCR, one page per CPU. Only for 32-bit kernel.
113 #endif /* _M_AMD64 */
114 
115  // KUSER_SHARED_DATA
117 #endif /* _X86_ */
118 }
119 
120 VOID
121 NTAPI
124 {
125  //
126  // Print the memory layout
127  //
128  DPRINT1(" 0x%p - 0x%p\t%s\n",
129  KSEG0_BASE,
131  "Boot Loaded Image");
132  DPRINT1(" 0x%p - 0x%p\t%s\n",
135  "PFN Database");
136  DPRINT1(" 0x%p - 0x%p\t%s\n",
139  "ARM3 Non Paged Pool");
140  DPRINT1(" 0x%p - 0x%p\t%s\n",
143  "System View Space");
144  DPRINT1(" 0x%p - 0x%p\t%s\n",
147  "Session Space");
148  DPRINT1(" 0x%p - 0x%p\t%s\n",
149  PTE_BASE, PTE_TOP,
150  "Page Tables");
151  DPRINT1(" 0x%p - 0x%p\t%s\n",
152  PDE_BASE, PDE_TOP,
153  "Page Directories");
154  DPRINT1(" 0x%p - 0x%p\t%s\n",
156  "Hyperspace");
157  DPRINT1(" 0x%p - 0x%p\t%s\n",
159  "System Cache");
160  DPRINT1(" 0x%p - 0x%p\t%s\n",
163  "ARM3 Paged Pool");
164  DPRINT1(" 0x%p - 0x%p\t%s\n",
166  "System PTE Space");
167  DPRINT1(" 0x%p - 0x%p\t%s\n",
169  "Non Paged Pool Expansion PTE Space");
170 }
171 
172 NTSTATUS
173 NTAPI
176 {
179  HANDLE ThreadHandle;
180 
181  /* Create the thread */
182  InitializeObjectAttributes(&ObjectAttributes, NULL, 0, NULL, NULL);
183  Status = PsCreateSystemThread(&ThreadHandle,
185  &ObjectAttributes,
186  NULL,
187  NULL,
189  NULL);
190 
191  /* Close the handle and return status */
192  ZwClose(ThreadHandle);
193  return Status;
194 }
195 
196 BOOLEAN
197 NTAPI
200  IN PLOADER_PARAMETER_BLOCK LoaderBlock)
201 {
202  extern MMPTE ValidKernelPte;
203  PMMPTE PointerPte;
205  PFN_NUMBER PageFrameNumber;
206 
207  /* Initialize the kernel address space */
208  ASSERT(Phase == 1);
209 
210  InitializeListHead(&MiSegmentList);
212  KeInitializeEvent(&MmWaitPageEvent, SynchronizationEvent, FALSE);
213  // Until we're fully demand paged, we can do things the old way through
214  // the balance manager
216 
217  MmKernelAddressSpace = &PsIdleProcess->Vm;
218 
219  /* Intialize system memory areas */
221 
222  /* Dump the address space */
224 
231 
232  //
233  // Create a PTE to double-map the shared data section. We allocate it
234  // from paged pool so that we can't fault when trying to touch the PTE
235  // itself (to map it), since paged pool addresses will already be mapped
236  // by the fault handler.
237  //
238  MmSharedUserDataPte = ExAllocatePoolWithTag(PagedPool,
239  sizeof(MMPTE),
240  TAG_MM);
241  if (!MmSharedUserDataPte) return FALSE;
242 
243  //
244  // Now get the PTE for shared data, and read the PFN that holds it
245  //
247  ASSERT(PointerPte->u.Hard.Valid == 1);
248  PageFrameNumber = PFN_FROM_PTE(PointerPte);
249 
250  /* Build the PTE and write it */
252  PointerPte,
253  MM_READONLY,
254  PageFrameNumber);
255  *MmSharedUserDataPte = TempPte;
256 
257  /* Initialize session working set support */
259 
260  /* Setup session IDs */
262 
263  /* Setup the memory threshold events */
264  if (!MiInitializeMemoryEvents()) return FALSE;
265 
266  /*
267  * Unmap low memory
268  */
270 
271  /* Initialize the balance set manager */
272  MmInitBsmThread();
273 
274  return TRUE;
275 }
276 
DWORD *typedef PVOID
Definition: winlogon.h:52
KEVENT MmWaitPageEvent
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
#define IN
Definition: typedefs.h:38
#define THREAD_ALL_ACCESS
Definition: nt_native.h:1339
LIST_ENTRY MiSegmentList
Definition: data.c:86
#define MM_HAL_VA_START
Definition: ketypes.h:256
#define TRUE
Definition: types.h:120
VOID NTAPI MiInitializeUserPfnBitmap(VOID)
Definition: freelist.c:44
VOID FORCEINLINE MmInitGlobalKernelPageDirectory(VOID)
Definition: mm.h:296
#define MM_HAL_VA_END
Definition: ketypes.h:257
PVOID MmSessionBase
Definition: mminit.c:134
FAST_MUTEX MiGlobalPageOperation
Definition: swapout.c:74
PMMPTE MmSharedUserDataPte
Definition: mminit.c:28
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel)?(CompletionRoutine!=NULL):TRUE)
#define MC_USER
Definition: mm.h:94
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
return STATUS_SUCCESS
Definition: btrfs.c:2690
_In_ KPRIORITY Priority
Definition: kefuncs.h:516
UCHAR MmDisablePagingExecutive
Definition: mminit.c:27
#define MEMORY_AREA_STATIC
Definition: mm.h:74
SIZE_T MmSizeOfNonPagedPoolInBytes
Definition: mminit.c:26
union _MMPTE::@2171 u
VOID INIT_FUNCTION NTAPI MiCreateArm3StaticMemoryArea(PVOID BaseAddress, SIZE_T Size, BOOLEAN Executable)
Definition: mminit.c:45
PMMPFN MmPfnDatabase
Definition: freelist.c:24
uint32_t ULONG_PTR
Definition: typedefs.h:63
PFN_NUMBER MxPfnAllocation
Definition: mminit.c:187
VOID NTAPI MiInitializeSessionWsSupport(VOID)
Definition: session.c:40
PEPROCESS PsIdleProcess
Definition: psmgr.c:51
VOID NTAPI INIT_FUNCTION MiDbgDumpAddressSpace(VOID)
Definition: mminit.c:123
VOID NTAPI MmInitializeMemoryConsumer(ULONG Consumer, NTSTATUS(*Trim)(ULONG Target, ULONG Priority, PULONG NrFreed))
Definition: balance.c:82
VOID NTAPI MmInitPagingFile(VOID)
Definition: pagefile.c:346
#define MiAddressToPte(x)
Definition: mmx86.c:19
ULONG PFN_NUMBER
Definition: ke.h:8
#define TAG_MM
Definition: tag.h:136
#define MEMORY_AREA_OWNED_BY_ARM3
Definition: mm.h:73
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
NTSTATUS NTAPI MmInitSectionImplementation(VOID)
Definition: section.c:2780
#define FALSE
Definition: types.h:117
PVOID MiSessionSpaceEnd
Definition: mminit.c:127
#define PCR
Definition: ke.h:8
#define KSEG0_BASE
Definition: ketypes.h:273
#define PDE_BASE
Definition: winldr.c:20
PVOID MiSystemViewStart
Definition: mminit.c:154
VOID NTAPI MiInitializeSessionIds(VOID)
Definition: session.c:117
SIZE_T MmBootImageSize
Definition: mminit.c:222
smooth NULL
Definition: ftsmooth.c:416
UINTN Size
Definition: acefiex.h:555
#define MI_DEBUG_MAPPING
Definition: mm.h:19
FAST_MUTEX
Definition: extypes.h:17
unsigned char BOOLEAN
PFN_COUNT MmNumberOfSystemPtes
Definition: mminit.c:181
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
LONG NTSTATUS
Definition: precomp.h:26
PVOID MmSystemCacheEnd
Definition: mminit.c:244
ULONG64 Valid
Definition: mmtypes.h:150
FORCEINLINE VOID ExInitializeFastMutex(_Out_ PFAST_MUTEX FastMutex)
Definition: exfuncs.h:274
HARDWARE_PTE_ARMV6 TempPte
Definition: winldr.c:75
MMPTE ValidKernelPte
Definition: init.c:31
_Must_inspect_result_ typedef _In_ ULONG _In_ BOOLEAN Target
Definition: iotypes.h:1067
PVOID MmPagedPoolStart
Definition: mminit.c:104
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
BOOLEAN NTAPI INIT_FUNCTION MiInitializeMemoryEvents(VOID)
Definition: mminit.c:1339
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define HYPER_SPACE_END
Definition: mm.h:14
unsigned char UCHAR
Definition: xmlstorage.h:181
BOOLEAN NTAPI INIT_FUNCTION MmInitSystem(IN ULONG Phase, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: mminit.c:199
VOID INIT_FUNCTION NTAPI MiInitSystemMemoryAreas(VOID)
Definition: mminit.c:67
#define PDE_TOP
Definition: mm.h:32
PVOID MmNonPagedSystemStart
Definition: mminit.c:96
#define KI_USER_SHARED_DATA
NTSTATUS MiRosTrimCache(ULONG Target, ULONG Priority, PULONG NrFreed)
Definition: swapout.c:633
VOID NTAPI MiInitBalancerThread(VOID)
Definition: balance.c:449
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define PTE_BASE
Definition: mmx86.c:14
Definition: typedefs.h:117
NTSTATUS NTAPI INIT_FUNCTION MmInitBsmThread(VOID)
Definition: mminit.c:175
ULONG MmReadClusterSize
Definition: mminit.c:23
VOID NTAPI KeBalanceSetManager(IN PVOID Context)
Definition: balmgr.c:137
CCHAR KeNumberProcessors
Definition: krnlinit.c:35
MMSUPPORT Vm
Definition: pstypes.h:1287
BOOLEAN Mm64BitPhysicalAddress
Definition: mminit.c:22
Status
Definition: gdiplustypes.h:24
PVOID MmSystemCacheStart
Definition: mminit.c:243
ULONG_PTR SIZE_T
Definition: typedefs.h:78
MMPTE_HARDWARE Hard
Definition: mmtypes.h:217
FORCEINLINE PMMSUPPORT MmGetKernelAddressSpace(VOID)
Definition: mm.h:1413
DWORD *typedef HANDLE
Definition: winlogon.h:52
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
NTSTATUS MmTrimUserMemory(ULONG Target, ULONG Priority, PULONG NrFreedPages)
Definition: balance.c:178
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
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
unsigned int * PULONG
Definition: retypes.h:1
#define DPRINT1
Definition: precomp.h:8
PVOID MmNonPagedPoolEnd
Definition: mminit.c:99
#define PTE_TOP
Definition: mm.h:33
#define MC_CACHE
Definition: mm.h:93
#define HYPER_SPACE
Definition: mm.h:13
unsigned int ULONG
Definition: retypes.h:1
SIZE_T MmSizeOfPagedPoolInBytes
Definition: mminit.c:110
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define MM_READONLY
Definition: miarm.h:48
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
FORCEINLINE VOID MI_MAKE_HARDWARE_PTE_KERNEL(IN PMMPTE NewPte, IN PMMPTE MappingPte, IN ULONG_PTR ProtectionMask, IN PFN_NUMBER PageFrameNumber)
Definition: miarm.h:748
PVOID MmNonPagedPoolExpansionStart
Definition: mminit.c:98
PVOID MmNonPagedPoolStart
Definition: mminit.c:97
VOID NTAPI MmInitializeRmapList(VOID)
Definition: rmap.c:43
SIZE_T MmSystemViewSize
Definition: mminit.c:155
#define PFN_FROM_PTE(v)
Definition: mm.h:88
PMMSUPPORT MmKernelAddressSpace
Definition: mminit.c:29
#define INIT_FUNCTION
Definition: ntoskrnl.h:11
#define PAGE_READWRITE
Definition: nt_native.h:1304