ReactOS  0.4.15-dev-2979-gfd8baca
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 1450 of file sysldr.c.

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

1429 {
1430  PMMPTE PointerPte;
1431  PFN_NUMBER PagesFreed;
1432 
1433  /* Get the start PTE */
1434  PointerPte = MiAddressToPte(InitStart);
1435  ASSERT(MI_IS_PHYSICAL_ADDRESS(InitStart) == FALSE);
1436 
1437  /* Compute the number of pages we expect to free */
1438  PagesFreed = (PFN_NUMBER)(MiAddressToPte(InitEnd) - PointerPte);
1439 
1440  /* Try to actually free them */
1441  PagesFreed = MiDeleteSystemPageableVm(PointerPte,
1442  PagesFreed,
1443  0,
1444  NULL);
1445 }
#define MiAddressToPte(x)
Definition: mmx86.c:19
ULONG PFN_NUMBER
Definition: ke.h:9
#define FALSE
Definition: types.h:117
#define ASSERT(a)
Definition: mode.c:44
PFN_COUNT NTAPI MiDeleteSystemPageableVm(IN PMMPTE PointerPte, IN PFN_NUMBER PageCount, IN ULONG Flags, OUT PPFN_NUMBER ValidPages)
Definition: virtual.c:275
#define NULL
Definition: types.h:112
FORCEINLINE BOOLEAN MI_IS_PHYSICAL_ADDRESS(IN PVOID Address)
Definition: miarm.h:950

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 pages: %lx\n", MmAvailablePages);
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);
69  OldIrql = MiAcquirePfnLock();
70 
71  while (TRUE)
72  {
74  {
76  MiReleasePfnLock(OldIrql);
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;
98  MiReleasePfnLock(OldIrql);
99 
100  ZeroAddress = MiMapPagesInZeroSpace(Pfn1, 1);
101  ASSERT(ZeroAddress);
102  RtlZeroMemory(ZeroAddress, PAGE_SIZE);
103  MiUnmapPagesInZeroSpace(ZeroAddress, 1);
104 
105  OldIrql = MiAcquirePfnLock();
106 
108  }
109  }
110 }
KEVENT MmZeroingPageEvent
Definition: zeropage.c:20
#define TRUE
Definition: types.h:120
VOID NTAPI MiFindInitializationCode(OUT PVOID *StartVa, OUT PVOID *EndVa)
Definition: sysldr.c:1450
PFN_NUMBER Flink
Definition: mm.h:366
PFN_NUMBER MmAvailablePages
Definition: freelist.c:26
MMPFNLIST MmZeroedPageListHead
Definition: pfnlist.c:41
PFN_NUMBER Flink
Definition: mm.h:434
UCHAR KIRQL
Definition: env_spec_w32.h:591
ULONG PFN_NUMBER
Definition: ke.h:9
#define FALSE
Definition: types.h:117
#define MI_SET_PROCESS2(x)
Definition: mm.h:308
VOID NTAPI MiInsertPageInList(IN PMMPFNLIST ListHead, IN PFN_NUMBER PageFrameIndex)
Definition: pfnlist.c:781
#define MI_SET_USAGE(x)
Definition: mm.h:306
KIRQL OldIrql
Definition: mm.h:1502
VOID NTAPI MiFreeInitializationCode(IN PVOID StartVa, IN PVOID EndVa)
Definition: sysldr.c:1427
#define ASSERT(a)
Definition: mode.c:44
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2652
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
PVOID NTAPI MiMapPagesInZeroSpace(IN PMMPFN Pfn1, IN PFN_NUMBER NumberOfPages)
Definition: hypermap.c:111
Definition: mm.h:362
#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:1000
#define MI_GET_PAGE_COLOR(x)
Definition: miarm.h:236
PFN_NUMBER NTAPI MiRemoveAnyPage(IN ULONG Color)
Definition: pfnlist.c:477
VOID NTAPI MiUnmapPagesInZeroSpace(IN PVOID VirtualAddress, IN PFN_NUMBER NumberOfPages)
Definition: hypermap.c:183
KPRIORITY NTAPI KeSetPriorityThread(IN PKTHREAD Thread, IN KPRIORITY Priority)
Definition: thrdobj.c:1327
PFN_NUMBER Total
Definition: mm.h:432
#define NULL
Definition: types.h:112
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define DPRINT
Definition: sndvol32.h:71
#define KeGetCurrentThread
Definition: hal.h:55
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
union _MMPFN::@1777 u1

Referenced by Phase1Initialization().

Variable Documentation

◆ MmZeroingPageEvent

KEVENT MmZeroingPageEvent

Definition at line 20 of file zeropage.c.

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