-/*
+/*
* PROJECT: ReactOS Kernel
* LICENSE: GPL - See COPYING in the top level directory
* FILE: ntoskrnl/mm/mminit.c
* PURPOSE: Memory Manager Initialization
- * PROGRAMMERS:
+ * PROGRAMMERS:
*/
/* INCLUDES ******************************************************************/
VOID NTAPI MiInitializeUserPfnBitmap(VOID);
-PCHAR
-MemType[] =
-{
- "ExceptionBlock ",
- "SystemBlock ",
- "Free ",
- "Bad ",
- "LoadedProgram ",
- "FirmwareTemporary ",
- "FirmwarePermanent ",
- "OsloaderHeap ",
- "OsloaderStack ",
- "SystemCode ",
- "HalCode ",
- "BootDriver ",
- "ConsoleInDriver ",
- "ConsoleOutDriver ",
- "StartupDpcStack ",
- "StartupKernelStack",
- "StartupPanicStack ",
- "StartupPcrPage ",
- "StartupPdrPage ",
- "RegistryData ",
- "MemoryData ",
- "NlsData ",
- "SpecialMemory ",
- "BBTMemory ",
- "LoaderReserve ",
- "LoaderXIPRom "
-};
-
HANDLE MpwThreadHandle;
KEVENT MpwThreadEvent;
UCHAR MmDisablePagingExecutive = 1; // Forced to off
PMMPTE MmSharedUserDataPte;
PMMSUPPORT MmKernelAddressSpace;
-BOOLEAN MiDbgEnableMdDump =
-#ifdef _ARM_
-TRUE;
-#else
-FALSE;
-#endif
+
+extern KEVENT MmWaitPageEvent;
+extern FAST_MUTEX MiGlobalPageOperation;
+extern LIST_ENTRY MiSegmentList;
+extern NTSTATUS MiRosTrimCache(ULONG Target, ULONG Priority, PULONG NrFreed);
/* PRIVATE FUNCTIONS *********************************************************/
MiInitSystemMemoryAreas()
{
PVOID BaseAddress;
- PHYSICAL_ADDRESS BoundaryAddressMultiple;
PMEMORY_AREA MArea;
NTSTATUS Status;
- BoundaryAddressMultiple.QuadPart = 0;
-
+
+ //
+ // Create the memory area to define the loader mappings
+ //
+ BaseAddress = (PVOID)KSEG0_BASE;
+ Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
+ MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
+ &BaseAddress,
+ MmBootImageSize,
+ PAGE_EXECUTE_READWRITE,
+ &MArea,
+ TRUE,
+ 0,
+ PAGE_SIZE);
+ ASSERT(Status == STATUS_SUCCESS);
+
//
// Create the memory area to define the PTE base
//
Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
&BaseAddress,
- 4 * 1024 * 1024,
+ PTE_TOP - PTE_BASE + 1,
PAGE_READWRITE,
&MArea,
TRUE,
0,
- BoundaryAddressMultiple);
+ PAGE_SIZE);
ASSERT(Status == STATUS_SUCCESS);
-
+
//
// Create the memory area to define Hyperspace
//
Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
&BaseAddress,
- 4 * 1024 * 1024,
+ HYPER_SPACE_END - HYPER_SPACE + 1,
PAGE_READWRITE,
&MArea,
TRUE,
0,
- BoundaryAddressMultiple);
+ PAGE_SIZE);
ASSERT(Status == STATUS_SUCCESS);
-
+
//
// Protect the PFN database
//
&MArea,
TRUE,
0,
- BoundaryAddressMultiple);
+ PAGE_SIZE);
ASSERT(Status == STATUS_SUCCESS);
-
+
//
// ReactOS requires a memory area to keep the initial NP area off-bounds
//
&MArea,
TRUE,
0,
- BoundaryAddressMultiple);
+ PAGE_SIZE);
ASSERT(Status == STATUS_SUCCESS);
-
+
//
// And we need one more for the system NP
//
Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
MEMORY_AREA_OWNED_BY_ARM3 | MEMORY_AREA_STATIC,
&BaseAddress,
- (ULONG_PTR)MmNonPagedPoolEnd -
- (ULONG_PTR)MmNonPagedSystemStart,
+ MiNonPagedSystemSize,
PAGE_READWRITE,
&MArea,
TRUE,
0,
- BoundaryAddressMultiple);
+ PAGE_SIZE);
ASSERT(Status == STATUS_SUCCESS);
-
+
//
// We also need one for system view space
//
&MArea,
TRUE,
0,
- BoundaryAddressMultiple);
+ PAGE_SIZE);
ASSERT(Status == STATUS_SUCCESS);
-
+
//
// And another for session space
//
&MArea,
TRUE,
0,
- BoundaryAddressMultiple);
+ PAGE_SIZE);
ASSERT(Status == STATUS_SUCCESS);
-
+
//
// One more for ARM paged pool
//
&MArea,
TRUE,
0,
- BoundaryAddressMultiple);
+ PAGE_SIZE);
ASSERT(Status == STATUS_SUCCESS);
-
+#ifndef _M_AMD64
//
// Next, the KPCR
//
&MArea,
TRUE,
0,
- BoundaryAddressMultiple);
+ PAGE_SIZE);
ASSERT(Status == STATUS_SUCCESS);
-
+#endif
//
// Now the KUSER_SHARED_DATA
//
&MArea,
TRUE,
0,
- BoundaryAddressMultiple);
+ PAGE_SIZE);
ASSERT(Status == STATUS_SUCCESS);
//
&MArea,
TRUE,
0,
- BoundaryAddressMultiple);
+ PAGE_SIZE);
ASSERT(Status == STATUS_SUCCESS);
#if defined(_X86_)
&MArea,
TRUE,
0,
- BoundaryAddressMultiple);
+ PAGE_SIZE);
ASSERT(Status == STATUS_SUCCESS);
#endif
}
VOID
NTAPI
+INIT_FUNCTION
MiDbgDumpAddressSpace(VOID)
{
//
// Print the memory layout
//
DPRINT1(" 0x%p - 0x%p\t%s\n",
- MmSystemRangeStart,
- (ULONG_PTR)MmSystemRangeStart + MmBootImageSize,
+ KSEG0_BASE,
+ (ULONG_PTR)KSEG0_BASE + MmBootImageSize,
"Boot Loaded Image");
DPRINT1(" 0x%p - 0x%p\t%s\n",
MmPfnDatabase,
DPRINT1(" 0x%p - 0x%p\t%s\n",
MmNonPagedPoolStart,
(ULONG_PTR)MmNonPagedPoolStart + MmSizeOfNonPagedPoolInBytes,
- "ARM³ Non Paged Pool");
+ "ARM3 Non Paged Pool");
DPRINT1(" 0x%p - 0x%p\t%s\n",
MiSystemViewStart,
(ULONG_PTR)MiSystemViewStart + MmSystemViewSize,
- "System View Space");
+ "System View Space");
DPRINT1(" 0x%p - 0x%p\t%s\n",
MmSessionBase,
MiSessionSpaceEnd,
"Session Space");
DPRINT1(" 0x%p - 0x%p\t%s\n",
- PTE_BASE, PDE_BASE,
+ PTE_BASE, PTE_TOP,
"Page Tables");
DPRINT1(" 0x%p - 0x%p\t%s\n",
- PDE_BASE, HYPER_SPACE,
+ PDE_BASE, PDE_TOP,
"Page Directories");
DPRINT1(" 0x%p - 0x%p\t%s\n",
- HYPER_SPACE, HYPER_SPACE + (4 * 1024 * 1024),
+ HYPER_SPACE, HYPER_SPACE_END,
"Hyperspace");
DPRINT1(" 0x%p - 0x%p\t%s\n",
MmPagedPoolStart,
(ULONG_PTR)MmPagedPoolStart + MmSizeOfPagedPoolInBytes,
- "ARM³ Paged Pool");
+ "ARM3 Paged Pool");
DPRINT1(" 0x%p - 0x%p\t%s\n",
MmNonPagedSystemStart, MmNonPagedPoolExpansionStart,
"System PTE Space");
VOID
NTAPI
-MiDbgDumpMemoryDescriptors(VOID)
-{
- PLIST_ENTRY NextEntry;
- PMEMORY_ALLOCATION_DESCRIPTOR Md;
- ULONG TotalPages = 0;
-
- DPRINT1("Base\t\tLength\t\tType\n");
- for (NextEntry = KeLoaderBlock->MemoryDescriptorListHead.Flink;
- NextEntry != &KeLoaderBlock->MemoryDescriptorListHead;
- NextEntry = NextEntry->Flink)
- {
- Md = CONTAINING_RECORD(NextEntry, MEMORY_ALLOCATION_DESCRIPTOR, ListEntry);
- DPRINT1("%08lX\t%08lX\t%s\n", Md->BasePage, Md->PageCount, MemType[Md->MemoryType]);
- TotalPages += Md->PageCount;
- }
-
- DPRINT1("Total: %08lX (%d MB)\n", TotalPages, (TotalPages * PAGE_SIZE) / 1024 / 1024);
-}
-
-NTSTATUS NTAPI
-MmMpwThreadMain(PVOID Ignored)
+MmMpwThreadMain(PVOID Parameter)
{
NTSTATUS Status;
ULONG PagesWritten;
LARGE_INTEGER Timeout;
+ UNREFERENCED_PARAMETER(Parameter);
+
Timeout.QuadPart = -50000000;
for(;;)
{
DbgPrint("MpwThread: Wait failed\n");
KeBugCheck(MEMORY_MANAGEMENT);
- return(STATUS_UNSUCCESSFUL);
+ return;
}
PagesWritten = 0;
- CcRosFlushDirtyPages(128, &PagesWritten);
+#ifndef NEWCC
+ // XXX arty -- we flush when evicting pages or destorying cache
+ // sections.
+ CcRosFlushDirtyPages(128, &PagesWritten, FALSE);
+#endif
}
}
NTSTATUS
NTAPI
+INIT_FUNCTION
MmInitMpwThread(VOID)
{
KPRIORITY Priority;
NTSTATUS Status;
CLIENT_ID MpwThreadId;
-
+
KeInitializeEvent(&MpwThreadEvent, SynchronizationEvent, FALSE);
Status = PsCreateSystemThread(&MpwThreadHandle,
NULL,
NULL,
&MpwThreadId,
- (PKSTART_ROUTINE) MmMpwThreadMain,
+ MmMpwThreadMain,
NULL);
if (!NT_SUCCESS(Status))
{
NTSTATUS
NTAPI
+INIT_FUNCTION
MmInitBsmThread(VOID)
{
NTSTATUS Status;
BOOLEAN
NTAPI
+INIT_FUNCTION
MmInitSystem(IN ULONG Phase,
IN PLOADER_PARAMETER_BLOCK LoaderBlock)
{
PMMPTE PointerPte;
MMPTE TempPte = ValidKernelPte;
PFN_NUMBER PageFrameNumber;
-
- if (Phase == 0)
- {
- /* Initialize the kernel address space */
- KeInitializeGuardedMutex(&PsGetCurrentProcess()->AddressCreationLock);
- MmKernelAddressSpace = MmGetCurrentAddressSpace();
- MmInitGlobalKernelPageDirectory();
-
- /* Dump memory descriptors */
- if (MiDbgEnableMdDump) MiDbgDumpMemoryDescriptors();
-
- /* Initialize ARM³ in phase 0 */
- MmArmInitSystem(0, KeLoaderBlock);
-
- /* Intialize system memory areas */
- MiInitSystemMemoryAreas();
-
- /* Dump the address space */
- MiDbgDumpAddressSpace();
- }
- else if (Phase == 1)
- {
- MiInitializeUserPfnBitmap();
- MmInitializeMemoryConsumer(MC_USER, MmTrimUserMemory);
- MmInitializeRmapList();
- MmInitializePageOp();
- MmInitSectionImplementation();
- MmInitPagingFile();
-
- //
- // Create a PTE to double-map the shared data section. We allocate it
- // from paged pool so that we can't fault when trying to touch the PTE
- // itself (to map it), since paged pool addresses will already be mapped
- // by the fault handler.
- //
- MmSharedUserDataPte = ExAllocatePoolWithTag(PagedPool,
- sizeof(MMPTE),
- ' mM');
- if (!MmSharedUserDataPte) return FALSE;
-
- //
- // Now get the PTE for shared data, and read the PFN that holds it
- //
- PointerPte = MiAddressToPte((PVOID)KI_USER_SHARED_DATA);
- ASSERT(PointerPte->u.Hard.Valid == 1);
- PageFrameNumber = PFN_FROM_PTE(PointerPte);
-
- /* Build the PTE and write it */
- MI_MAKE_HARDWARE_PTE_KERNEL(&TempPte,
- PointerPte,
- MM_READONLY,
- PageFrameNumber);
- *MmSharedUserDataPte = TempPte;
-
- /* Setup the memory threshold events */
- if (!MiInitializeMemoryEvents()) return FALSE;
-
- /*
- * Unmap low memory
- */
- MiInitBalancerThread();
-
- /*
- * Initialise the modified page writer.
- */
- MmInitMpwThread();
-
- /* Initialize the balance set manager */
- MmInitBsmThread();
- }
+
+ /* Initialize the kernel address space */
+ ASSERT(Phase == 1);
+
+ InitializeListHead(&MiSegmentList);
+ ExInitializeFastMutex(&MiGlobalPageOperation);
+ KeInitializeEvent(&MmWaitPageEvent, SynchronizationEvent, FALSE);
+ // Until we're fully demand paged, we can do things the old way through
+ // the balance manager
+ MmInitializeMemoryConsumer(MC_CACHE, MiRosTrimCache);
+
+ MmKernelAddressSpace = &PsIdleProcess->Vm;
+
+ /* Intialize system memory areas */
+ MiInitSystemMemoryAreas();
+
+ /* Dump the address space */
+ MiDbgDumpAddressSpace();
+
+ MmInitGlobalKernelPageDirectory();
+ MiInitializeUserPfnBitmap();
+ MmInitializeMemoryConsumer(MC_USER, MmTrimUserMemory);
+ MmInitializeRmapList();
+ MmInitSectionImplementation();
+ MmInitPagingFile();
+
+ //
+ // Create a PTE to double-map the shared data section. We allocate it
+ // from paged pool so that we can't fault when trying to touch the PTE
+ // itself (to map it), since paged pool addresses will already be mapped
+ // by the fault handler.
+ //
+ MmSharedUserDataPte = ExAllocatePoolWithTag(PagedPool,
+ sizeof(MMPTE),
+ ' mM');
+ if (!MmSharedUserDataPte) return FALSE;
+
+ //
+ // Now get the PTE for shared data, and read the PFN that holds it
+ //
+ PointerPte = MiAddressToPte((PVOID)KI_USER_SHARED_DATA);
+ ASSERT(PointerPte->u.Hard.Valid == 1);
+ PageFrameNumber = PFN_FROM_PTE(PointerPte);
+
+ /* Build the PTE and write it */
+ MI_MAKE_HARDWARE_PTE_KERNEL(&TempPte,
+ PointerPte,
+ MM_READONLY,
+ PageFrameNumber);
+ *MmSharedUserDataPte = TempPte;
+
+ /* Initialize session working set support */
+ MiInitializeSessionWsSupport();
+
+ /* Setup session IDs */
+ MiInitializeSessionIds();
+
+ /* Setup the memory threshold events */
+ if (!MiInitializeMemoryEvents()) return FALSE;
+
+ /*
+ * Unmap low memory
+ */
+ MiInitBalancerThread();
+
+ /*
+ * Initialise the modified page writer.
+ */
+ MmInitMpwThread();
+
+ /* Initialize the balance set manager */
+ MmInitBsmThread();
return TRUE;
}