ReactOS  0.4.15-dev-1377-ga59cecd
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 CODE_SEG("INIT")
43 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);
61  // TODO: Perhaps it would be prudent to bugcheck here, not only assert?
62 }
63 
64 CODE_SEG("INIT")
65 VOID
66 NTAPI
68 {
69  //
70  // Create all the static memory areas.
71  //
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 }
124 
125 CODE_SEG("INIT")
126 VOID
127 NTAPI
129 {
130  //
131  // Print the memory layout
132  //
133  DPRINT1(" 0x%p - 0x%p\t%s\n",
134  KSEG0_BASE,
136  "Boot Loaded Image");
137  DPRINT1(" 0x%p - 0x%p\t%s\n",
140  "PFN Database");
141  DPRINT1(" 0x%p - 0x%p\t%s\n",
144  "ARM3 Non Paged Pool");
145  DPRINT1(" 0x%p - 0x%p\t%s\n",
148  "System View Space");
149  DPRINT1(" 0x%p - 0x%p\t%s\n",
152  "Session Space");
153  DPRINT1(" 0x%p - 0x%p\t%s\n",
154  PTE_BASE, PTE_TOP,
155  "Page Tables");
156  DPRINT1(" 0x%p - 0x%p\t%s\n",
157  PDE_BASE, PDE_TOP,
158  "Page Directories");
159  DPRINT1(" 0x%p - 0x%p\t%s\n",
161  "Hyperspace");
162  DPRINT1(" 0x%p - 0x%p\t%s\n",
164  "System Cache");
165  DPRINT1(" 0x%p - 0x%p\t%s\n",
168  "ARM3 Paged Pool");
169  DPRINT1(" 0x%p - 0x%p\t%s\n",
171  "System PTE Space");
172  DPRINT1(" 0x%p - 0x%p\t%s\n",
174  "Non Paged Pool Expansion PTE Space");
175 }
176 
177 CODE_SEG("INIT")
178 NTSTATUS
179 NTAPI
181 {
184  HANDLE ThreadHandle;
185 
186  /* Create the thread */
188  Status = PsCreateSystemThread(&ThreadHandle,
191  NULL,
192  NULL,
194  NULL);
195 
196  /* Close the handle and return status */
197  ZwClose(ThreadHandle);
198  return Status;
199 }
200 
201 CODE_SEG("INIT")
202 BOOLEAN
203 NTAPI
205  IN PLOADER_PARAMETER_BLOCK LoaderBlock)
206 {
207  extern MMPTE ValidKernelPte;
208  PMMPTE PointerPte;
210  PFN_NUMBER PageFrameNumber;
211  PLIST_ENTRY ListEntry;
212  PLDR_DATA_TABLE_ENTRY DataTableEntry;
213 
214  /* Initialize the kernel address space */
215  ASSERT(Phase == 1);
216 
220  // Until we're fully demand paged, we can do things the old way through
221  // the balance manager
223 
225 
226  /* Intialize system memory areas */
228 
229  /* Dump the address space */
231 
238 
239  //
240  // Create a PTE to double-map the shared data section. We allocate it
241  // from paged pool so that we can't fault when trying to touch the PTE
242  // itself (to map it), since paged pool addresses will already be mapped
243  // by the fault handler.
244  //
246  sizeof(MMPTE),
247  TAG_MM);
248  if (!MmSharedUserDataPte) return FALSE;
249 
250  //
251  // Now get the PTE for shared data, and read the PFN that holds it
252  //
254  ASSERT(PointerPte->u.Hard.Valid == 1);
255  PageFrameNumber = PFN_FROM_PTE(PointerPte);
256 
257  /* Build the PTE and write it */
259  PointerPte,
260  MM_READONLY,
261  PageFrameNumber);
263 
264  /* Initialize session working set support */
266 
267  /* Setup session IDs */
269 
270  /* Setup the memory threshold events */
271  if (!MiInitializeMemoryEvents()) return FALSE;
272 
273  /*
274  * Unmap low memory
275  */
277 
278  /* Initialize the balance set manager */
279  MmInitBsmThread();
280 
281  /* Loop the boot loaded images (under lock) */
283  for (ListEntry = PsLoadedModuleList.Flink;
284  ListEntry != &PsLoadedModuleList;
285  ListEntry = ListEntry->Flink)
286  {
287  /* Get the data table entry */
288  DataTableEntry = CONTAINING_RECORD(ListEntry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);
289 
290  /* Set up the image protection */
291  MiWriteProtectSystemImage(DataTableEntry->DllBase);
292  }
294 
295  return TRUE;
296 }
297 
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: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
#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
#define TRUE
Definition: types.h:120
#define MC_USER
Definition: mm.h:94
union _MMPTE::@2312 u
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
VOID NTAPI MiWriteProtectSystemImage(_In_ PVOID ImageBase)
Definition: sysldr.c:2446
BOOLEAN NTAPI MiInitializeMemoryEvents(VOID)
Definition: mminit.c:1339
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI MmInitBsmThread(VOID)
Definition: mminit.c:180
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
UCHAR MmDisablePagingExecutive
Definition: mminit.c:27
#define MEMORY_AREA_STATIC
Definition: mm.h:74
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:75
VOID NTAPI MmInitPagingFile(VOID)
Definition: pagefile.c:255
#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:2804
#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:21
PVOID MiSystemViewStart
Definition: mminit.c:154
VOID NTAPI MiInitializeSessionIds(VOID)
Definition: session.c:117
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:204
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:45
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
HARDWARE_PTE_ARMV6 TempPte
Definition: winldr.c:76
CODE_SEG("INIT")
Definition: fsrtlpc.c:19
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:410
#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:442
#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:23
VOID NTAPI KeBalanceSetManager(IN PVOID Context)
Definition: balmgr.c:137
CCHAR KeNumberProcessors
Definition: krnlinit.c:35
MMSUPPORT Vm
Definition: pstypes.h:1298
BOOLEAN Mm64BitPhysicalAddress
Definition: mminit.c:22
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:1445
VOID NTAPI MiInitSystemMemoryAreas(VOID)
Definition: mminit.c:67
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
NTSTATUS MmTrimUserMemory(ULONG Target, ULONG Priority, PULONG NrFreedPages)
Definition: balance.c:171
#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:128
PVOID MmNonPagedPoolEnd
Definition: mminit.c:99
FORCEINLINE VOID MmInitGlobalKernelPageDirectory(VOID)
Definition: mm.h:300
#define PTE_TOP
Definition: mm.h:154
#define MC_CACHE
Definition: mm.h:93
#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:45
#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:774
PVOID MmNonPagedPoolExpansionStart
Definition: mminit.c:98
PVOID MmNonPagedPoolStart
Definition: mminit.c:97
VOID NTAPI MmInitializeRmapList(VOID)
Definition: rmap.c:39
SIZE_T MmSystemViewSize
Definition: mminit.c:155
#define PFN_FROM_PTE(v)
Definition: mm.h:92
PMMSUPPORT MmKernelAddressSpace
Definition: mminit.c:29
#define PAGE_READWRITE
Definition: nt_native.h:1304