ReactOS  0.4.15-dev-1187-g119f102
zeropage.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
#include <mm/ARM3/miarm.h>
Include dependency graph for zeropage.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#define MODULE_INVOLVED_IN_ARM3
 

Functions

VOID NTAPI MiFindInitializationCode (OUT PVOID *StartVa, OUT PVOID *EndVa)
 
VOID NTAPI MiFreeInitializationCode (IN PVOID StartVa, IN PVOID EndVa)
 
VOID NTAPI MmZeroPageThread (VOID)
 

Variables

KEVENT MmZeroingPageEvent
 

Macro Definition Documentation

◆ MODULE_INVOLVED_IN_ARM3

#define MODULE_INVOLVED_IN_ARM3

Definition at line 15 of file zeropage.c.

◆ NDEBUG

#define NDEBUG

Definition at line 12 of file zeropage.c.

Function Documentation

◆ MiFindInitializationCode()

VOID NTAPI MiFindInitializationCode ( OUT PVOID StartVa,
OUT PVOID EndVa 
)

Definition at line 1451 of file sysldr.c.

1453 {
1454  ULONG Size, SectionCount, Alignment;
1455  PLDR_DATA_TABLE_ENTRY LdrEntry;
1456  ULONG_PTR DllBase, InitStart, InitEnd, ImageEnd, InitCode;
1457  PLIST_ENTRY NextEntry;
1458  PIMAGE_NT_HEADERS NtHeader;
1459  PIMAGE_SECTION_HEADER Section, LastSection, InitSection;
1460  BOOLEAN InitFound;
1461  DBG_UNREFERENCED_LOCAL_VARIABLE(InitSection);
1462 
1463  /* So we don't free our own code yet */
1464  InitCode = (ULONG_PTR)&MiFindInitializationCode;
1465 
1466  /* Assume failure */
1467  *StartVa = NULL;
1468 
1469  /* Acquire the necessary locks while we loop the list */
1473  KernelMode,
1474  FALSE,
1475  NULL);
1477 
1478  /* Loop all loaded modules */
1479  NextEntry = PsLoadedModuleList.Flink;
1480  while (NextEntry != &PsLoadedModuleList)
1481  {
1482  /* Get the loader entry and its DLL base */
1483  LdrEntry = CONTAINING_RECORD(NextEntry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);
1484  DllBase = (ULONG_PTR)LdrEntry->DllBase;
1485 
1486  /* Only process boot loaded images. Other drivers are processed by
1487  MmFreeDriverInitialization */
1488  if (LdrEntry->Flags & LDRP_MM_LOADED)
1489  {
1490  /* Keep going */
1491  NextEntry = NextEntry->Flink;
1492  continue;
1493  }
1494 
1495  /* Get the NT header */
1496  NtHeader = RtlImageNtHeader((PVOID)DllBase);
1497  if (!NtHeader)
1498  {
1499  /* Keep going */
1500  NextEntry = NextEntry->Flink;
1501  continue;
1502  }
1503 
1504  /* Get the first section, the section count, and scan them all */
1505  Section = IMAGE_FIRST_SECTION(NtHeader);
1506  SectionCount = NtHeader->FileHeader.NumberOfSections;
1507  InitStart = 0;
1508  while (SectionCount > 0)
1509  {
1510  /* Assume failure */
1511  InitFound = FALSE;
1512 
1513  /* Is this the INIT section or a discardable section? */
1514  if ((strncmp((PCCH)Section->Name, "INIT", 5) == 0) ||
1516  {
1517  /* Remember this */
1518  InitFound = TRUE;
1519  InitSection = Section;
1520  }
1521 
1522  if (InitFound)
1523  {
1524  /* Pick the biggest size -- either raw or virtual */
1525  Size = max(Section->SizeOfRawData, Section->Misc.VirtualSize);
1526 
1527  /* Read the section alignment */
1529 
1530  /* Get the start and end addresses */
1531  InitStart = DllBase + Section->VirtualAddress;
1532  InitEnd = ALIGN_UP_BY(InitStart + Size, Alignment);
1533 
1534  /* Align the addresses to PAGE_SIZE */
1535  InitStart = ALIGN_UP_BY(InitStart, PAGE_SIZE);
1536  InitEnd = ALIGN_DOWN_BY(InitEnd, PAGE_SIZE);
1537 
1538  /* Have we reached the last section? */
1539  if (SectionCount == 1)
1540  {
1541  /* Remember this */
1542  LastSection = Section;
1543  }
1544  else
1545  {
1546  /* We have not, loop all the sections */
1547  LastSection = NULL;
1548  do
1549  {
1550  /* Keep going until we find a non-discardable section range */
1551  SectionCount--;
1552  Section++;
1554  {
1555  /* Discardable, so record it, then keep going */
1556  LastSection = Section;
1557  }
1558  else
1559  {
1560  /* Non-contigous discard flag, or no flag, break out */
1561  break;
1562  }
1563  }
1564  while (SectionCount > 1);
1565  }
1566 
1567  /* Have we found a discardable or init section? */
1568  if (LastSection)
1569  {
1570  /* Pick the biggest size -- either raw or virtual */
1571  Size = max(LastSection->SizeOfRawData, LastSection->Misc.VirtualSize);
1572 
1573  /* Use this as the end of the section address */
1574  InitEnd = DllBase + LastSection->VirtualAddress + Size;
1575 
1576  /* Have we reached the last section yet? */
1577  if (SectionCount != 1)
1578  {
1579  /* Then align this accross the session boundary */
1580  InitEnd = ALIGN_UP_BY(InitEnd, Alignment);
1581  InitEnd = ALIGN_DOWN_BY(InitEnd, PAGE_SIZE);
1582  }
1583  }
1584 
1585  /* Make sure we don't let the init section go past the image */
1586  ImageEnd = DllBase + LdrEntry->SizeOfImage;
1587  if (InitEnd > ImageEnd) InitEnd = ALIGN_UP_BY(ImageEnd, PAGE_SIZE);
1588 
1589  /* Make sure we have a valid, non-zero init section */
1590  if (InitStart < InitEnd)
1591  {
1592  /* Make sure we are not within this code itself */
1593  if ((InitCode >= InitStart) && (InitCode < InitEnd))
1594  {
1595  /* Return it, we can't free ourselves now */
1596  ASSERT(*StartVa == 0);
1597  *StartVa = (PVOID)InitStart;
1598  *EndVa = (PVOID)InitEnd;
1599  }
1600  else
1601  {
1602  /* This isn't us -- go ahead and free it */
1603  ASSERT(MI_IS_PHYSICAL_ADDRESS((PVOID)InitStart) == FALSE);
1604  DPRINT("Freeing init code: %p-%p ('%wZ' @%p : '%s')\n",
1605  (PVOID)InitStart,
1606  (PVOID)InitEnd,
1607  &LdrEntry->BaseDllName,
1608  LdrEntry->DllBase,
1609  InitSection->Name);
1610  MiFreeInitializationCode((PVOID)InitStart, (PVOID)InitEnd);
1611  }
1612  }
1613  }
1614 
1615  /* Move to the next section */
1616  SectionCount--;
1617  Section++;
1618  }
1619 
1620  /* Move to the next module */
1621  NextEntry = NextEntry->Flink;
1622  }
1623 
1624  /* Release the locks and return */
1628 }
ERESOURCE PsLoadedModuleResource
Definition: sysldr.c:37
#define max(a, b)
Definition: svc.c:63
#define TRUE
Definition: types.h:120
LIST_ENTRY PsLoadedModuleList
Definition: sysldr.c:34
ULONG SizeOfImage
Definition: ldrtypes.h:143
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
uint32_t ULONG_PTR
Definition: typedefs.h:65
BOOLEAN NTAPI ExAcquireResourceExclusiveLite(IN PERESOURCE Resource, IN BOOLEAN Wait)
Definition: resource.c:770
PVOID DllBase
Definition: btrfs_drv.h:1926
VOID NTAPI MiFreeInitializationCode(IN PVOID InitStart, IN PVOID InitEnd)
Definition: sysldr.c:1428
#define FALSE
Definition: types.h:117
#define IMAGE_FIRST_SECTION(NtHeader)
Definition: ntimage.h:427
union Alignment_ Alignment
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
union _IMAGE_SECTION_HEADER::@1553 Misc
void DPRINT(...)
Definition: polytest.cpp:61
void * PVOID
Definition: retypes.h:9
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183
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
#define DBG_UNREFERENCED_LOCAL_VARIABLE(L)
Definition: ntbasedef.h:320
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
CONST CHAR * PCCH
Definition: ntbasedef.h:393
if(!(yy_init))
Definition: macro.lex.yy.c:714
int strncmp(const char *String1, const char *String2, ACPI_SIZE Count)
Definition: utclib.c:534
VOID FASTCALL ExReleaseResourceLite(IN PERESOURCE Resource)
Definition: resource.c:1817
KMUTANT MmSystemLoadLock
Definition: sysldr.c:39
#define MUTANT_INCREMENT
Definition: extypes.h:84
LONG NTAPI KeReleaseMutant(IN PKMUTANT Mutant, IN KPRIORITY Increment, IN BOOLEAN Abandon, IN BOOLEAN Wait)
Definition: mutex.c:98
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define KeEnterCriticalRegion()
Definition: ke_x.h:83
Definition: btrfs_drv.h:1922
#define PAGE_SIZE
Definition: env_spec_w32.h:49
Definition: typedefs.h:119
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
#define ALIGN_DOWN_BY(size, align)
#define KeLeaveCriticalRegion()
Definition: ke_x.h:114
UNICODE_STRING BaseDllName
Definition: ldrtypes.h:145
BYTE Name[IMAGE_SIZEOF_SHORT_NAME]
Definition: pedump.c:281
#define RtlImageNtHeader
Definition: compat.h:554
ULONG Flags
Definition: ntddk_ex.h:207
unsigned int ULONG
Definition: retypes.h:1
#define ULONG_PTR
Definition: config.h:101
#define ALIGN_UP_BY(size, align)
INIT_FUNCTION VOID NTAPI MiFindInitializationCode(OUT PVOID *StartVa, OUT PVOID *EndVa)
Definition: sysldr.c:1451
FORCEINLINE BOOLEAN MI_IS_PHYSICAL_ADDRESS(IN PVOID Address)
Definition: miarm.h:928
#define LDRP_MM_LOADED
Definition: ldrtypes.h:60
#define IMAGE_SCN_MEM_DISCARDABLE
Definition: ntimage.h:235

Referenced by MiFindInitializationCode(), and MmZeroPageThread().

◆ MiFreeInitializationCode()

VOID NTAPI MiFreeInitializationCode ( IN PVOID  StartVa,
IN PVOID  EndVa 
)

Definition at line 1428 of file sysldr.c.

1430 {
1431  PMMPTE PointerPte;
1432  PFN_NUMBER PagesFreed;
1433 
1434  /* Get the start PTE */
1435  PointerPte = MiAddressToPte(InitStart);
1436  ASSERT(MI_IS_PHYSICAL_ADDRESS(InitStart) == FALSE);
1437 
1438  /* Compute the number of pages we expect to free */
1439  PagesFreed = (PFN_NUMBER)(MiAddressToPte(InitEnd) - PointerPte);
1440 
1441  /* Try to actually free them */
1442  PagesFreed = MiDeleteSystemPageableVm(PointerPte,
1443  PagesFreed,
1444  0,
1445  NULL);
1446 }
#define MiAddressToPte(x)
Definition: mmx86.c:19
ULONG PFN_NUMBER
Definition: ke.h:8
#define FALSE
Definition: types.h:117
smooth NULL
Definition: ftsmooth.c:416
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
PFN_COUNT NTAPI MiDeleteSystemPageableVm(IN PMMPTE PointerPte, IN PFN_NUMBER PageCount, IN ULONG Flags, OUT PPFN_NUMBER ValidPages)
Definition: virtual.c:297
FORCEINLINE BOOLEAN MI_IS_PHYSICAL_ADDRESS(IN PVOID Address)
Definition: miarm.h:928

Referenced by MiFindInitializationCode(), and MmZeroPageThread().

◆ MmZeroPageThread()

VOID NTAPI MmZeroPageThread ( VOID  )

Definition at line 36 of file zeropage.c.

37 {
39  PVOID StartAddress, EndAddress;
40  PVOID WaitObjects[2];
41  KIRQL OldIrql;
42  PVOID ZeroAddress;
43  PFN_NUMBER PageIndex, FreePage;
44  PMMPFN Pfn1;
45 
46  /* Get the discardable sections to free them */
47  MiFindInitializationCode(&StartAddress, &EndAddress);
48  if (StartAddress) MiFreeInitializationCode(StartAddress, EndAddress);
49  DPRINT("Free non-cache pages: %lx\n", MmAvailablePages + MiMemoryConsumers[MC_CACHE].PagesUsed);
50 
51  /* Set our priority to 0 */
52  Thread->BasePriority = 0;
54 
55  /* Setup the wait objects */
56  WaitObjects[0] = &MmZeroingPageEvent;
57 // WaitObjects[1] = &PoSystemIdleTimer; FIXME: Implement idle timer
58 
59  while (TRUE)
60  {
62  WaitObjects,
63  WaitAny,
64  WrFreePage,
65  KernelMode,
66  FALSE,
67  NULL,
68  NULL);
70 
71  while (TRUE)
72  {
74  {
77  break;
78  }
79 
80  PageIndex = MmFreePageListHead.Flink;
81  ASSERT(PageIndex != LIST_HEAD);
82  Pfn1 = MiGetPfnEntry(PageIndex);
84  MI_SET_PROCESS2("Kernel 0 Loop");
86 
87  /* The first global free page should also be the first on its own list */
88  if (FreePage != PageIndex)
89  {
90  KeBugCheckEx(PFN_LIST_CORRUPT,
91  0x8F,
92  FreePage,
93  PageIndex,
94  0);
95  }
96 
97  Pfn1->u1.Flink = LIST_HEAD;
99 
100  ZeroAddress = MiMapPagesInZeroSpace(Pfn1, 1);
101  ASSERT(ZeroAddress);
102  RtlZeroMemory(ZeroAddress, PAGE_SIZE);
103  MiUnmapPagesInZeroSpace(ZeroAddress, 1);
104 
106 
108  }
109  }
110 }
KEVENT MmZeroingPageEvent
Definition: zeropage.c:20
MM_MEMORY_CONSUMER MiMemoryConsumers[MC_MAXIMUM]
Definition: balance.c:36
#define TRUE
Definition: types.h:120
VOID NTAPI MiFindInitializationCode(OUT PVOID *StartVa, OUT PVOID *EndVa)
Definition: sysldr.c:1451
PFN_NUMBER Flink
Definition: mm.h:309
FORCEINLINE KIRQL MiAcquirePfnLock(VOID)
Definition: mm.h:901
PFN_NUMBER MmAvailablePages
Definition: freelist.c:26
MMPFNLIST MmZeroedPageListHead
Definition: pfnlist.c:41
FORCEINLINE VOID MiReleasePfnLock(_In_ KIRQL OldIrql)
Definition: mm.h:908
PFN_NUMBER Flink
Definition: mm.h:373
UCHAR KIRQL
Definition: env_spec_w32.h:591
ULONG PFN_NUMBER
Definition: ke.h:8
#define FALSE
Definition: types.h:117
#define MI_SET_PROCESS2(x)
Definition: mm.h:254
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
VOID NTAPI MiInsertPageInList(IN PMMPFNLIST ListHead, IN PFN_NUMBER PageFrameIndex)
Definition: pfnlist.c:778
#define MI_SET_USAGE(x)
Definition: mm.h:253
VOID NTAPI MiFreeInitializationCode(IN PVOID StartVa, IN PVOID EndVa)
Definition: sysldr.c:1428
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
NTSTATUS NTAPI KeWaitForMultipleObjects(IN ULONG Count, IN PVOID Object[], IN WAIT_TYPE WaitType, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL, OUT PKWAIT_BLOCK WaitBlockArray OPTIONAL)
Definition: wait.c:586
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
PVOID NTAPI MiMapPagesInZeroSpace(IN PMMPFN Pfn1, IN PFN_NUMBER NumberOfPages)
Definition: hypermap.c:113
union _MMPFN::@1774 u1
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:790
Definition: mm.h:305
#define PAGE_SIZE
Definition: env_spec_w32.h:49
LIST_HEAD(acpi_bus_event_list)
FORCEINLINE PMMPFN MiGetPfnEntry(IN PFN_NUMBER Pfn)
Definition: mm.h:940
#define MI_GET_PAGE_COLOR(x)
Definition: miarm.h:237
PFN_NUMBER NTAPI MiRemoveAnyPage(IN ULONG Color)
Definition: pfnlist.c:475
VOID NTAPI MiUnmapPagesInZeroSpace(IN PVOID VirtualAddress, IN PFN_NUMBER NumberOfPages)
Definition: hypermap.c:185
KPRIORITY NTAPI KeSetPriorityThread(IN PKTHREAD Thread, IN KPRIORITY Priority)
Definition: thrdobj.c:1327
PFN_NUMBER Total
Definition: mm.h:371
#define MC_CACHE
Definition: mm.h:93
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define KeGetCurrentThread
Definition: hal.h:44
VOID NTAPI KeClearEvent(IN PKEVENT Event)
Definition: eventobj.c:22
MMPFNLIST MmFreePageListHead
Definition: pfnlist.c:42
VOID NTAPI KeBugCheckEx(_In_ ULONG BugCheckCode, _In_ ULONG_PTR BugCheckParameter1, _In_ ULONG_PTR BugCheckParameter2, _In_ ULONG_PTR BugCheckParameter3, _In_ ULONG_PTR BugCheckParameter4)
Definition: rtlcompat.c:108

Referenced by Phase1Initialization().

Variable Documentation

◆ MmZeroingPageEvent

KEVENT MmZeroingPageEvent

Definition at line 20 of file zeropage.c.

Referenced by MiInsertPageInFreeList(), MmArmInitSystem(), and MmZeroPageThread().