ReactOS  r73918
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 
27 //
28 // 0 | 1 is on/off paging, 2 is undocumented
29 //
30 UCHAR MmDisablePagingExecutive = 1; // Forced to off
33 
34 extern KEVENT MmWaitPageEvent;
38 
39 /* PRIVATE FUNCTIONS *********************************************************/
40 
41 //
42 // Helper function to create initial memory areas.
43 // The created area is always read/write.
44 //
45 VOID
47 NTAPI
49 {
50  const ULONG Protection = Executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
51  PVOID pBaseAddress = BaseAddress;
52  PMEMORY_AREA MArea;
54 
57  &pBaseAddress,
58  Size,
59  Protection,
60  &MArea,
61  0,
62  PAGE_SIZE);
63  ASSERT(Status == STATUS_SUCCESS);
64  // TODO: Perhaps it would be prudent to bugcheck here, not only assert?
65 }
66 
67 VOID
69 NTAPI
71 {
72  //
73  // Create all the static memory areas.
74  //
75 
76  // The loader mappings. The only Executable area.
78 
79  // The PTE base
81 
82  // Hyperspace
84 
85  // Protect the PFN database
87 
88  // ReactOS requires a memory area to keep the initial NP area off-bounds
90 
91  // System PTE space
93 
94  // Nonpaged pool expansion space
96 
97  // System view space
99 
100  // Session space
102 
103  // Paged pool
105 
106  // Debugger mapping
108 
109 #if defined(_X86_)
110  // Reserved HAL area (includes KUSER_SHARED_DATA and KPCR)
112 #else /* _X86_ */
113 #ifndef _M_AMD64
114  // KPCR, one page per CPU. Only for 32-bit kernel.
116 #endif /* _M_AMD64 */
117 
118  // KUSER_SHARED_DATA
120 #endif /* _X86_ */
121 }
122 
123 VOID
124 NTAPI
127 {
128  //
129  // Print the memory layout
130  //
131  DPRINT1(" 0x%p - 0x%p\t%s\n",
132  KSEG0_BASE,
134  "Boot Loaded Image");
135  DPRINT1(" 0x%p - 0x%p\t%s\n",
138  "PFN Database");
139  DPRINT1(" 0x%p - 0x%p\t%s\n",
142  "ARM3 Non Paged Pool");
143  DPRINT1(" 0x%p - 0x%p\t%s\n",
146  "System View Space");
147  DPRINT1(" 0x%p - 0x%p\t%s\n",
150  "Session Space");
151  DPRINT1(" 0x%p - 0x%p\t%s\n",
152  PTE_BASE, PTE_TOP,
153  "Page Tables");
154  DPRINT1(" 0x%p - 0x%p\t%s\n",
155  PDE_BASE, PDE_TOP,
156  "Page Directories");
157  DPRINT1(" 0x%p - 0x%p\t%s\n",
159  "Hyperspace");
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 VOID
173 NTAPI
175 {
177 #ifndef NEWCC
178  ULONG PagesWritten;
179 #endif
181 
182  UNREFERENCED_PARAMETER(Parameter);
183 
184  Timeout.QuadPart = -50000000;
185 
186  for(;;)
187  {
188  Status = KeWaitForSingleObject(&MpwThreadEvent,
189  0,
190  KernelMode,
191  FALSE,
192  &Timeout);
193  if (!NT_SUCCESS(Status))
194  {
195  DbgPrint("MpwThread: Wait failed\n");
196  KeBugCheck(MEMORY_MANAGEMENT);
197  return;
198  }
199 
200 #ifndef NEWCC
201  PagesWritten = 0;
202 
203  // XXX arty -- we flush when evicting pages or destorying cache
204  // sections.
205  CcRosFlushDirtyPages(128, &PagesWritten, FALSE);
206 #endif
207  }
208 }
209 
210 NTSTATUS
211 NTAPI
214 {
217  CLIENT_ID MpwThreadId;
218 
219  KeInitializeEvent(&MpwThreadEvent, SynchronizationEvent, FALSE);
220 
223  NULL,
224  NULL,
225  &MpwThreadId,
227  NULL);
228  if (!NT_SUCCESS(Status))
229  {
230  return(Status);
231  }
232 
233  Priority = 27;
236  &Priority,
237  sizeof(Priority));
238 
239  return(STATUS_SUCCESS);
240 }
241 
242 NTSTATUS
243 NTAPI
246 {
249  HANDLE ThreadHandle;
250 
251  /* Create the thread */
252  InitializeObjectAttributes(&ObjectAttributes, NULL, 0, NULL, NULL);
253  Status = PsCreateSystemThread(&ThreadHandle,
255  &ObjectAttributes,
256  NULL,
257  NULL,
259  NULL);
260 
261  /* Close the handle and return status */
262  ZwClose(ThreadHandle);
263  return Status;
264 }
265 
266 BOOLEAN
267 NTAPI
270  IN PLOADER_PARAMETER_BLOCK LoaderBlock)
271 {
272  extern MMPTE ValidKernelPte;
273  PMMPTE PointerPte;
275  PFN_NUMBER PageFrameNumber;
276 
277  /* Initialize the kernel address space */
278  ASSERT(Phase == 1);
279 
280  InitializeListHead(&MiSegmentList);
282  KeInitializeEvent(&MmWaitPageEvent, SynchronizationEvent, FALSE);
283  // Until we're fully demand paged, we can do things the old way through
284  // the balance manager
286 
287  MmKernelAddressSpace = &PsIdleProcess->Vm;
288 
289  /* Intialize system memory areas */
291 
292  /* Dump the address space */
294 
301 
302  //
303  // Create a PTE to double-map the shared data section. We allocate it
304  // from paged pool so that we can't fault when trying to touch the PTE
305  // itself (to map it), since paged pool addresses will already be mapped
306  // by the fault handler.
307  //
308  MmSharedUserDataPte = ExAllocatePoolWithTag(PagedPool,
309  sizeof(MMPTE),
310  TAG_MM);
311  if (!MmSharedUserDataPte) return FALSE;
312 
313  //
314  // Now get the PTE for shared data, and read the PFN that holds it
315  //
317  ASSERT(PointerPte->u.Hard.Valid == 1);
318  PageFrameNumber = PFN_FROM_PTE(PointerPte);
319 
320  /* Build the PTE and write it */
322  PointerPte,
323  MM_READONLY,
324  PageFrameNumber);
325  *MmSharedUserDataPte = TempPte;
326 
327  /* Initialize session working set support */
329 
330  /* Setup session IDs */
332 
333  /* Setup the memory threshold events */
334  if (!MiInitializeMemoryEvents()) return FALSE;
335 
336  /*
337  * Unmap low memory
338  */
340 
341  /*
342  * Initialise the modified page writer.
343  */
344  MmInitMpwThread();
345 
346  /* Initialize the balance set manager */
347  MmInitBsmThread();
348 
349  return TRUE;
350 }
351 
DWORD *typedef PVOID
Definition: winlogon.h:52
KEVENT MmWaitPageEvent
#define STATUS_SUCCESS
Definition: contextmenu.cpp:55
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:39
#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
VOID NTAPI MiInitializeUserPfnBitmap(VOID)
Definition: freelist.c:44
VOID FORCEINLINE MmInitGlobalKernelPageDirectory(VOID)
Definition: mm.h:276
NTSTATUS NTAPI INIT_FUNCTION MmInitMpwThread(VOID)
Definition: mminit.c:213
#define DbgPrint
Definition: loader.c:26
#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:31
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel)?(CompletionRoutine!=NULL):TRUE)
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:315
#define MC_USER
Definition: mm.h:92
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
_In_ KPRIORITY Priority
Definition: kefuncs.h:516
#define TRUE
Definition: numbers.c:17
KEVENT MpwThreadEvent
Definition: mminit.c:23
UCHAR MmDisablePagingExecutive
Definition: mminit.c:30
_In_ PVOID Parameter
Definition: ldrtypes.h:239
#define MEMORY_AREA_STATIC
Definition: mm.h:72
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
SIZE_T MmSizeOfNonPagedPoolInBytes
Definition: mminit.c:26
VOID NTAPI MmMpwThreadMain(PVOID Parameter)
Definition: mminit.c:174
LONG KPRIORITY
Definition: compat.h:454
PMMPFN MmPfnDatabase
Definition: freelist.c:24
uint32_t ULONG_PTR
Definition: typedefs.h:64
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:126
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
NTSTATUS NTAPI CcRosFlushDirtyPages(ULONG Target, PULONG Count, BOOLEAN Wait)
Definition: view.c:160
ULONG PFN_NUMBER
Definition: ke.h:8
#define TAG_MM
Definition: tag.h:136
#define MEMORY_AREA_OWNED_BY_ARM3
Definition: mm.h:71
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:2788
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
#define NULL
Definition: mystdio.h:57
VOID NTAPI MiInitializeSessionIds(VOID)
Definition: session.c:117
SIZE_T MmBootImageSize
Definition: mminit.c:222
HANDLE MpwThreadHandle
Definition: mminit.c:22
VOID NTAPI KeBugCheck(ULONG BugCheckCode)
Definition: bug.c:1469
NTSTATUS NTAPI NtSetInformationThread(IN HANDLE ThreadHandle, IN THREADINFOCLASS ThreadInformationClass, IN PVOID ThreadInformation, IN ULONG ThreadInformationLength)
Definition: query.c:1966
UINTN Size
Definition: acefiex.h:550
#define MI_DEBUG_MAPPING
Definition: mm.h:22
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
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:412
BOOLEAN NTAPI INIT_FUNCTION MiInitializeMemoryEvents(VOID)
Definition: mminit.c:1329
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define HYPER_SPACE_END
Definition: mm.h:13
unsigned char UCHAR
Definition: xmlstorage.h:181
BOOLEAN NTAPI INIT_FUNCTION MmInitSystem(IN ULONG Phase, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: mminit.c:269
VOID INIT_FUNCTION NTAPI MiInitSystemMemoryAreas(VOID)
Definition: mminit.c:70
#define PDE_TOP
Definition: mm.h:30
PVOID MmNonPagedSystemStart
Definition: mminit.c:96
VOID INIT_FUNCTION NTAPI MiCreateArm3StaticMemoryArea(PVOID BaseAddress, ULONG Size, BOOLEAN Executable)
Definition: mminit.c:48
#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:118
NTSTATUS NTAPI INIT_FUNCTION MmInitBsmThread(VOID)
Definition: mminit.c:245
ULONG MmReadClusterSize
Definition: mminit.c:26
VOID NTAPI KeBalanceSetManager(IN PVOID Context)
Definition: balmgr.c:137
CCHAR KeNumberProcessors
Definition: krnlinit.c:35
MMSUPPORT Vm
Definition: pstypes.h:1233
BOOLEAN Mm64BitPhysicalAddress
Definition: mminit.c:25
Status
Definition: gdiplustypes.h:24
MMPTE_HARDWARE Hard
Definition: mmtypes.h:217
union _MMPTE::@1885 u
#define NT_SUCCESS(StatCode)
Definition: cmd.c:149
FORCEINLINE PMMSUPPORT MmGetKernelAddressSpace(VOID)
Definition: mm.h:1376
DWORD *typedef HANDLE
Definition: winlogon.h:52
LONG NTSTATUS
Definition: DriverTester.h:11
static ULONG Timeout
Definition: ping.c:61
#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:31
#define MC_CACHE
Definition: mm.h:91
#define FALSE
Definition: numbers.c:16
#define HYPER_SPACE
Definition: mm.h:12
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:46
#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:746
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:82
PMMSUPPORT MmKernelAddressSpace
Definition: mminit.c:32
LONGLONG QuadPart
Definition: typedefs.h:113
#define INIT_FUNCTION
Definition: ntoskrnl.h:11
#define PAGE_READWRITE
Definition: nt_native.h:1304