ReactOS  0.4.15-dev-3297-g037c744
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 //
25 UCHAR MmDisablePagingExecutive = 1; // Forced to off
28 
29 extern KEVENT MmWaitPageEvent;
33 
34 /* PRIVATE FUNCTIONS *********************************************************/
35 
36 //
37 // Helper function to create initial memory areas.
38 // The created area is always read/write.
39 //
40 CODE_SEG("INIT")
41 VOID
42 NTAPI
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 
62 CODE_SEG("INIT")
63 VOID
64 NTAPI
66 {
67  //
68  // Create all the static memory areas.
69  //
70 
71 #ifdef _M_AMD64
72  // Reserved range FFFF800000000000 - FFFFF68000000000
74 #endif /* _M_AMD64 */
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 CODE_SEG("INIT")
124 VOID
125 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",
162  "System Cache");
163  DPRINT1(" 0x%p - 0x%p\t%s\n",
166  "ARM3 Paged Pool");
167  DPRINT1(" 0x%p - 0x%p\t%s\n",
169  "System PTE Space");
170  DPRINT1(" 0x%p - 0x%p\t%s\n",
172  "Non Paged Pool Expansion PTE Space");
173 }
174 
175 CODE_SEG("INIT")
176 NTSTATUS
177 NTAPI
179 {
182  HANDLE ThreadHandle;
183 
184  /* Create the thread */
186  Status = PsCreateSystemThread(&ThreadHandle,
189  NULL,
190  NULL,
192  NULL);
193 
194  /* Close the handle and return status */
195  ZwClose(ThreadHandle);
196  return Status;
197 }
198 
199 CODE_SEG("INIT")
200 BOOLEAN
201 NTAPI
203  IN PLOADER_PARAMETER_BLOCK LoaderBlock)
204 {
205  extern MMPTE ValidKernelPte;
206  PMMPTE PointerPte;
208  PFN_NUMBER PageFrameNumber;
209  PLIST_ENTRY ListEntry;
210  PLDR_DATA_TABLE_ENTRY DataTableEntry;
211 
212  /* Initialize the kernel address space */
213  ASSERT(Phase == 1);
214 
215 #ifdef NEWCC
219  // Until we're fully demand paged, we can do things the old way through
220  // the balance manager
221  // CcInitView will override this...
223 #else
225 #endif
226 
228 
229  /* Intialize system memory areas */
231 
232  /* Dump the address space */
234 
240 
241  //
242  // Create a PTE to double-map the shared data section. We allocate it
243  // from paged pool so that we can't fault when trying to touch the PTE
244  // itself (to map it), since paged pool addresses will already be mapped
245  // by the fault handler.
246  //
248  sizeof(MMPTE),
249  TAG_MM);
250  if (!MmSharedUserDataPte) return FALSE;
251 
252  //
253  // Now get the PTE for shared data, and read the PFN that holds it
254  //
256  ASSERT(PointerPte->u.Hard.Valid == 1);
257  PageFrameNumber = PFN_FROM_PTE(PointerPte);
258 
259  /* Build the PTE and write it */
261  PointerPte,
262  MM_READONLY,
263  PageFrameNumber);
265 
266  /* Initialize session working set support */
268 
269  /* Setup session IDs */
271 
272  /* Setup the memory threshold events */
273  if (!MiInitializeMemoryEvents()) return FALSE;
274 
275  /*
276  * Unmap low memory
277  */
279 
280  /* Initialize the balance set manager */
281  MmInitBsmThread();
282 
283  /* Loop the boot loaded images (under lock) */
285  for (ListEntry = PsLoadedModuleList.Flink;
286  ListEntry != &PsLoadedModuleList;
287  ListEntry = ListEntry->Flink)
288  {
289  /* Get the data table entry */
290  DataTableEntry = CONTAINING_RECORD(ListEntry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);
291 
292  /* Set up the image protection */
293  MiWriteProtectSystemImage(DataTableEntry->DllBase);
294  }
296 
297  return TRUE;
298 }
299 
KEVENT MmWaitPageEvent
Definition: section.c:67
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:260
#define MM_HAL_VA_END
Definition: ketypes.h:261
PVOID MmSessionBase
Definition: mminit.c:134
FAST_MUTEX MiGlobalPageOperation
Definition: swapout.c:74
PMMPTE MmSharedUserDataPte
Definition: mminit.c:26
#define TRUE
Definition: types.h:120
#define MC_USER
Definition: mm.h:103
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
VOID NTAPI MiWriteProtectSystemImage(_In_ PVOID ImageBase)
Definition: sysldr.c:2443
BOOLEAN NTAPI MiInitializeMemoryEvents(VOID)
Definition: mminit.c:1339
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI MmInitBsmThread(VOID)
Definition: mminit.c:178
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
UCHAR MmDisablePagingExecutive
Definition: mminit.c:25
#define MEMORY_AREA_STATIC
Definition: mm.h:87
SIZE_T MmSizeOfNonPagedPoolInBytes
Definition: mminit.c:26
PMMPFN MmPfnDatabase
Definition: freelist.c:24
uint32_t ULONG_PTR
Definition: typedefs.h:65
PFN_NUMBER MxPfnAllocation
Definition: mminit.c:187
ERESOURCE PsLoadedModuleResource
Definition: sysldr.c:37
BOOLEAN NTAPI ExAcquireResourceExclusiveLite(IN PERESOURCE Resource, IN BOOLEAN Wait)
Definition: resource.c:770
VOID NTAPI MiInitializeSessionWsSupport(VOID)
Definition: session.c:40
PVOID DllBase
Definition: btrfs_drv.h:1926
PEPROCESS PsIdleProcess
Definition: psmgr.c:51
VOID NTAPI MmInitializeMemoryConsumer(ULONG Consumer, NTSTATUS(*Trim)(ULONG Target, ULONG Priority, PULONG NrFreed))
Definition: balance.c:58
VOID NTAPI MmInitPagingFile(VOID)
Definition: pagefile.c:255
#define MiAddressToPte(x)
Definition: mmx86.c:19
ULONG PFN_NUMBER
Definition: ke.h:9
#define TAG_MM
Definition: tag.h:136
#define MEMORY_AREA_OWNED_BY_ARM3
Definition: mm.h:86
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:2260
#define FALSE
Definition: types.h:117
PVOID MiSessionSpaceEnd
Definition: mminit.c:127
#define PCR
Definition: ke.h:8
#define KSEG0_BASE
Definition: ketypes.h:277
#define PDE_BASE
Definition: winldr.c:21
PVOID MiSystemViewStart
Definition: mminit.c:154
VOID NTAPI MiInitializeSessionIds(VOID)
Definition: session.c:116
SIZE_T MmBootImageSize
Definition: mminit.c:222
unsigned char BOOLEAN
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
BOOLEAN NTAPI MmInitSystem(IN ULONG Phase, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: mminit.c:202
Status
Definition: gdiplustypes.h:24
#define MI_DEBUG_MAPPING
Definition: mm.h:20
#define MM_READONLY
Definition: inbv.c:11
FAST_MUTEX
Definition: extypes.h:17
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
PFN_COUNT MmNumberOfSystemPtes
Definition: mminit.c:181
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
PVOID MmSystemCacheEnd
Definition: mminit.c:244
#define ASSERT(a)
Definition: mode.c:44
LIST_ENTRY PsLoadedModuleList
Definition: sysldr.c:34
ULONG64 Valid
Definition: mmtypes.h:150
FORCEINLINE VOID ExInitializeFastMutex(_Out_ PFAST_MUTEX FastMutex)
Definition: exfuncs.h:274
#define MI_REAL_SYSTEM_RANGE_START
Definition: mm.h:12
union _MMPTE::@2275 u
HARDWARE_PTE_ARMV6 TempPte
Definition: winldr.c:76
MMPTE ValidKernelPte
Definition: init.c:29
VOID FASTCALL ExReleaseResourceLite(IN PERESOURCE Resource)
Definition: resource.c:1817
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:401
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define HYPER_SPACE_END
Definition: mm.h:15
_In_ WDFINTERRUPT _In_ WDF_INTERRUPT_POLICY _In_ WDF_INTERRUPT_PRIORITY Priority
Definition: wdfinterrupt.h:651
unsigned char UCHAR
Definition: xmlstorage.h:181
#define PDE_TOP
Definition: mm.h:161
PVOID MmNonPagedSystemStart
Definition: mminit.c:96
#define KI_USER_SHARED_DATA
Definition: btrfs_drv.h:1922
NTSTATUS MiRosTrimCache(ULONG Target, ULONG Priority, PULONG NrFreed)
Definition: swapout.c:633
VOID NTAPI MiInitBalancerThread(VOID)
Definition: balance.c:361
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define PTE_BASE
Definition: mmx86.c:14
Definition: typedefs.h:119
ULONG MmReadClusterSize
Definition: mminit.c:21
VOID NTAPI KeBalanceSetManager(IN PVOID Context)
Definition: balmgr.c:137
CCHAR KeNumberProcessors
Definition: krnlinit.c:35
MMSUPPORT Vm
Definition: pstypes.h:1356
BOOLEAN Mm64BitPhysicalAddress
Definition: mminit.c:20
PVOID MmSystemCacheStart
Definition: mminit.c:243
ULONG_PTR SIZE_T
Definition: typedefs.h:80
MMPTE_HARDWARE Hard
Definition: mmtypes.h:217
FORCEINLINE PMMSUPPORT MmGetKernelAddressSpace(VOID)
Definition: mm.h:1640
VOID NTAPI MiInitSystemMemoryAreas(VOID)
Definition: mminit.c:65
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
NTSTATUS MmTrimUserMemory(ULONG Target, ULONG Priority, PULONG NrFreedPages)
Definition: balance.c:138
#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 NULL
Definition: types.h:112
_In_ WDFIOTARGET Target
Definition: wdfrequest.h:306
#define DPRINT1
Definition: precomp.h:8
VOID NTAPI MiDbgDumpAddressSpace(VOID)
Definition: mminit.c:126
PVOID MmNonPagedPoolEnd
Definition: mminit.c:99
#define PTE_TOP
Definition: mm.h:154
#define HYPER_SPACE
Definition: mm.h:14
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 PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
VOID NTAPI MiCreateArm3StaticMemoryArea(PVOID BaseAddress, SIZE_T Size, BOOLEAN Executable)
Definition: mminit.c:43
#define STATUS_SUCCESS
Definition: shellext.h:65
FORCEINLINE VOID MI_MAKE_HARDWARE_PTE_KERNEL(IN PMMPTE NewPte, IN PMMPTE MappingPte, IN ULONG_PTR ProtectionMask, IN PFN_NUMBER PageFrameNumber)
Definition: miarm.h:778
PVOID MmNonPagedPoolExpansionStart
Definition: mminit.c:98
PVOID MmNonPagedPoolStart
Definition: mminit.c:97
VOID NTAPI MmInitGlobalKernelPageDirectory(VOID)
Definition: page.c:277
VOID NTAPI MmInitializeRmapList(VOID)
Definition: rmap.c:38
static CODE_SEG("PAGE")
Definition: isapnp.c:1482
SIZE_T MmSystemViewSize
Definition: mminit.c:155
#define PFN_FROM_PTE(v)
Definition: mm.h:92
PMMSUPPORT MmKernelAddressSpace
Definition: mminit.c:27
#define PAGE_READWRITE
Definition: nt_native.h:1304