From d9e07f6e89f7eaaaadcc1b01c8b92095ef102b51 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A7=9C=E6=B5=B7=E5=A4=A9?= <1076168468@qq.com>
Date: Sat, 29 Oct 2022 15:33:43 +0800
Subject: [PATCH 1/2] =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=AE=8C=E6=88=90?=
 =?UTF-8?q?=EF=BC=8C=E6=8F=90=E4=BA=A4=E4=BD=9C=E4=B8=9A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 pfnlist.c |  10 +--
 sysproc.c | 207 ++++++++++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 182 insertions(+), 35 deletions(-)

diff --git a/pfnlist.c b/pfnlist.c
index fa77e0b..3a4ad74 100644
--- a/pfnlist.c
+++ b/pfnlist.c
@@ -145,13 +145,13 @@ MiAllocateAnyPages(
 		//
 		for (i = 0; i < NumberOfPages && MiFreePageCount > 0; i++) {
 
-			Pfn = MiFreePageListHead;
-			MiFreePageListHead = MiGetPfnDatabaseEntry(Pfn)->Next;
-			MiFreePageCount--;
+			Pfn = MiFreePageListHead;	//¼ÇÂ¼Ê×¸ö¿ÕÏÐÒ³
+			MiFreePageListHead = MiGetPfnDatabaseEntry(Pfn)->Next;	//½«¿ÕÏÐÒ³´ÓÁ´±íÖÐÈ¡ÏÂ
+			MiFreePageCount--;	//¼õÉÙ¿ÕÏÐÒ³×ÜÊýÁ¿¼ÇÂ¼
 
-			MiGetPfnDatabaseEntry(Pfn)->PageState = BUSY_PAGE;
+			MiGetPfnDatabaseEntry(Pfn)->PageState = BUSY_PAGE;	//½«¿ÕÏÐÒ³±ê¼ÇÎªBUSY_PAGE
 
-			PfnArray[i] = Pfn;
+			PfnArray[i] = Pfn;	//¼ÇÂ¼·µ»ØµÄ¿ÕÏÐÒ³µÄÒ³¿òºÅ
 		}
 
 		//
diff --git a/sysproc.c b/sysproc.c
index ba79659..957b1bf 100644
--- a/sysproc.c
+++ b/sysproc.c
@@ -953,25 +953,20 @@ ConsoleCmdResumeThread(
 // ÏÂÃæÊÇºÍ¿ØÖÆÌ¨ÃüÁî vm Ïà¹ØµÄ´úÂë¡£
 //
 
+/*
+Ìá¹©¸ÃÊ¾Àý´úÂëÊÇÎªÁË²ûÊÍÒ»¸ö¸ÅÄî£¬»òÕß½øÐÐÒ»¸ö²âÊÔ£¬²¢²»´ú±í×Å
+×î°²È«µÄ±àÂëÊµ¼ù£¬Òò´Ë²»Ó¦ÔÚÓ¦ÓÃ³ÌÐò»òÍøÕ¾ÖÐÊ¹ÓÃ¸ÃÊ¾Àý´úÂë¡£¶Ô
+ÓÚ³¬³ö±¾Ê¾Àý´úÂëµÄÔ¤ÆÚÓÃÍ¾ÒÔÍâµÄÊ¹ÓÃËùÔì³ÉµÄÅ¼È»»ò¼Ì·¢ÐÔËðÊ§£¬
+±±¾©Ó¢ÕæÊ±´ú¿Æ¼¼ÓÐÏÞ¹«Ë¾²»³Ðµ£ÈÎºÎÔðÈÎ¡£
+*/
+
+
 PRIVATE
 VOID
 ConsoleCmdVM(
 	IN HANDLE StdHandle,
 	IN PCSTR Arg
 	)
-/*++
-
-¹¦ÄÜÃèÊö£º
-	Í³¼ÆÊä³öÓÉ½ø³Ì ID Ö¸¶¨µÄ½ø³ÌµÄÐéÄâµØÖ·ÃèÊö·ûµÄÐÅÏ¢¡£¿ØÖÆÌ¨ÃüÁî¡°vm processid¡±¡£
-
-²ÎÊý£º
-	StdHandle -- ±ê×¼ÊäÈë¡¢Êä³ö¾ä±ú¡£
-	Arg -- ÃüÁî²ÎÊý×Ö·û´®¡£½ø³ÌµÄ ID¡£
-
-·µ»ØÖµ£º
-	ÎÞ¡£
-
---*/
 {
 	BOOL IntState;
 	ULONG ProcID;
@@ -982,6 +977,8 @@ ConsoleCmdVM(
 	ULONG Index, TotalVpnCount, AllocatedVpnCount, FreeVpnCount, VpnCount;
 	STATUS Status;
 	
+	const char* OutputFormat = NULL;
+	
 	//
 	// ´ÓÃüÁî²ÎÊý×Ö·û´®ÖÐ»ñµÃ½ø³Ì ID¡£
 	//
@@ -1010,7 +1007,8 @@ ConsoleCmdVM(
 	//
 	// Êä³ö VAD Á´±íÖÐ¼ÇÂ¼µÄÆðÊ¼Ò³¿òºÅ£¬½áÊøÒ³¿òºÅ
 	//
-	fprintf(StdHandle, "Total Vpn from %d to %d. (0x%X - 0x%X)\n\n",
+	OutputFormat = "Total Vpn from %d to %d. (0x%X - 0x%X)\n\n";
+	fprintf(StdHandle, OutputFormat,
 		pVadList->StartingVpn, pVadList->EndVpn,
 		pVadList->StartingVpn * PAGE_SIZE, (pVadList->EndVpn + 1) * PAGE_SIZE - 1);
 	
@@ -1026,7 +1024,8 @@ ConsoleCmdVM(
 		pVad = CONTAINING_RECORD(pListEntry, MMVAD, VadListEntry);
 		
 		VpnCount = pVad->EndVpn - pVad->StartingVpn + 1;
-		fprintf(StdHandle, "%d# Vad Include %d Vpn From %d to %d. (0x%X - 0x%X)\n",
+		OutputFormat = "%d# Vad Include %d Vpn From %d to %d. (0x%X - 0x%X)\n";
+		fprintf(StdHandle, OutputFormat,
 			Index, VpnCount, pVad->StartingVpn, pVad->EndVpn,
 			pVad->StartingVpn * PAGE_SIZE, (pVad->EndVpn + 1) * PAGE_SIZE - 1);
 		
@@ -1037,41 +1036,165 @@ ConsoleCmdVM(
 	// Í³¼ÆÐéÄâÒ³¿ò×ÜÊý¡¢ÒÑ·ÖÅäµÄÐéÄâÒ³¿òºÍÎ´·ÖÅäµÄÐéÄâÒ³¿ò
 	//
 	TotalVpnCount = pVadList->EndVpn - pVadList->StartingVpn + 1;
-	fprintf(StdHandle, "\nTotal Vpn Count: %d.\n", TotalVpnCount);
-	fprintf(StdHandle, "Allocated Vpn Count: %d.\n", AllocatedVpnCount);
+	OutputFormat = "\nTotal Vpn Count: %d.\n";
+	fprintf(StdHandle, OutputFormat, TotalVpnCount);
+	
+	OutputFormat = "Allocated Vpn Count: %d.\n";
+	fprintf(StdHandle, OutputFormat, AllocatedVpnCount);
+	
 	FreeVpnCount = TotalVpnCount - AllocatedVpnCount;
-	fprintf(StdHandle, "Free Vpn Count: %d.\n", FreeVpnCount);
+	OutputFormat = "Free Vpn Count: %d.\n\n";
+	fprintf(StdHandle, OutputFormat, FreeVpnCount);
+	
+	//
+	// Êä³öÎïÀíÒ³µÄÁãÒ³ÊýÁ¿ºÍ¿ÕÏÐÒ³ÊýÁ¿
+	//
+	OutputFormat = "Zeroed Physical Page Count: %d.\n";
+	fprintf(StdHandle, OutputFormat, MiZeroedPageCount);
+	
+	OutputFormat = "Free Physical Page Count: %d.\n\n";
+	fprintf(StdHandle, OutputFormat, MiFreePageCount);
+	
 	
+	//////////////////////////////////////////////////////////////////////////
+	//
+	// ·ÖÅäÒ»¿éÐÂµÄÐéÄâÄÚ´æ¡£µ«ÊÇÃ»ÓÐÊ¹ÓÃ MEM_COMMIT ±êÖ¾ÎªÆä·ÖÅäÎïÀíÒ³¡£
+	//
+	PVOID BaseAddress = 0;
+	SIZE_T RegionSize = 1;	
+	Status = MmAllocateVirtualMemory(&BaseAddress, &RegionSize, MEM_RESERVE, TRUE);
+	if (!EOS_SUCCESS(Status)) {
+		fprintf(StdHandle, "Allocate virtual memory at 0x%X faild.\n", BaseAddress);
+		goto VM_RETURN;
+	}
+	
+	//
+	// Êä³öÐÂ·ÖÅäµÄÄÚ´æµÄ»ùÖ·ºÍ´óÐ¡
+	//
+	OutputFormat = "New VM's base address: 0x%X. Size: 0x%X.\n\n";
+	fprintf(StdHandle, OutputFormat, BaseAddress, RegionSize);
+	
+	//
+	// ±éÀú VAD Á´±í£¬Êä³öËùÓÐ VAD µÄÆðÊ¼Ò³¿òºÅ£¬½áÊøÒ³¿òºÅºÍ°üº¬µÄÐéÄâÒ³¿òÊýÁ¿
+	//
+	Index = AllocatedVpnCount = 0;
+	for(pListEntry = pVadList->VadListHead.Next;
+		pListEntry != &pVadList->VadListHead;
+		pListEntry = pListEntry->Next) {
+	
+		Index++;
+		pVad = CONTAINING_RECORD(pListEntry, MMVAD, VadListEntry);
+		
+		VpnCount = pVad->EndVpn - pVad->StartingVpn + 1;
+		OutputFormat = "%d# Vad Include %d Vpn From %d to %d. (0x%X - 0x%X)\n";
+		fprintf(StdHandle, OutputFormat,
+			Index, VpnCount, pVad->StartingVpn, pVad->EndVpn,
+			pVad->StartingVpn * PAGE_SIZE, (pVad->EndVpn + 1) * PAGE_SIZE - 1);
+		
+		AllocatedVpnCount += VpnCount;
+	}
+	
+	//
+	// Í³¼ÆÒÑ·ÖÅäµÄÐéÄâÒ³¿òºÍÎ´·ÖÅäµÄÐéÄâÒ³¿ò
+	//
+	OutputFormat = "\nAllocated Vpn Count: %d.\n";
+	fprintf(StdHandle, OutputFormat, AllocatedVpnCount);
+	
+	FreeVpnCount = TotalVpnCount - AllocatedVpnCount;
+	OutputFormat = "Free Vpn Count: %d.\n\n";
+	fprintf(StdHandle, OutputFormat, FreeVpnCount);
+	
+	//
+	// Êä³öÎïÀíÒ³µÄÁãÒ³ÊýÁ¿ºÍ¿ÕÏÐÒ³ÊýÁ¿
+	//
+	OutputFormat = "Zeroed Physical Page Count: %d.\n";
+	fprintf(StdHandle, OutputFormat, MiZeroedPageCount);
+	
+	OutputFormat = "Free Physical Page Count: %d.\n\n";
+	fprintf(StdHandle, OutputFormat, MiFreePageCount);
+	
+	
+	//////////////////////////////////////////////////////////////////////////
+	//
+	// ÊÍ·Å¸Õ¸Õ·ÖÅäµÄÐéÄâÄÚ´æ¡£
+	//
+	RegionSize = 0;		// ËùÊÍ·ÅÐéÄâÄÚ´æµÄ´óÐ¡±ØÐë¸³ÖµÎª 0
+	MmFreeVirtualMemory(&BaseAddress, &RegionSize, MEM_RELEASE, TRUE);
+	
+	//
+	// Êä³öÊÍ·ÅµÄµÄÐéÄâÄÚ´æµÄ»ùÖ·ºÍ´óÐ¡
+	//
+	OutputFormat = "Free VM's base address: 0x%X. Size: 0x%X.\n\n";
+	fprintf(StdHandle, OutputFormat, BaseAddress, RegionSize);
+	
+	//
+	// ±éÀú VAD Á´±í£¬Êä³öËùÓÐ VAD µÄÆðÊ¼Ò³¿òºÅ£¬½áÊøÒ³¿òºÅºÍ°üº¬µÄÐéÄâÒ³¿òÊýÁ¿
+	//
+	Index = AllocatedVpnCount = 0;
+	for(pListEntry = pVadList->VadListHead.Next;
+		pListEntry != &pVadList->VadListHead;
+		pListEntry = pListEntry->Next) {
+	
+		Index++;
+		pVad = CONTAINING_RECORD(pListEntry, MMVAD, VadListEntry);
+		
+		VpnCount = pVad->EndVpn - pVad->StartingVpn + 1;
+		OutputFormat = "%d# Vad Include %d Vpn From %d to %d. (0x%X - 0x%X)\n";
+		fprintf(StdHandle, OutputFormat,
+			Index, VpnCount, pVad->StartingVpn, pVad->EndVpn,
+			pVad->StartingVpn * PAGE_SIZE, (pVad->EndVpn + 1) * PAGE_SIZE - 1);
+		
+		AllocatedVpnCount += VpnCount;
+	}
+	
+	//
+	// Í³¼ÆÒÑ·ÖÅäµÄÐéÄâÒ³¿òºÍÎ´·ÖÅäµÄÐéÄâÒ³¿ò
+	//
+	OutputFormat = "\nAllocated Vpn Count: %d.\n";
+	fprintf(StdHandle, OutputFormat, AllocatedVpnCount);
+	
+	FreeVpnCount = TotalVpnCount - AllocatedVpnCount;
+	OutputFormat = "Free Vpn Count: %d.\n\n";
+	fprintf(StdHandle, OutputFormat, FreeVpnCount);
+	
+	//
+	// Êä³öÎïÀíÒ³µÄÁãÒ³ÊýÁ¿ºÍ¿ÕÏÐÒ³ÊýÁ¿
+	//
+	OutputFormat = "Zeroed Physical Page Count: %d.\n";
+	fprintf(StdHandle, OutputFormat, MiZeroedPageCount);
+	
+	OutputFormat = "Free Physical Page Count: %d.\n\n";
+	fprintf(StdHandle, OutputFormat, MiFreePageCount);
+	
+VM_RETURN:	
 	KeEnableInterrupts(IntState);	// ¿ªÖÐ¶Ï
 	
 	ObDerefObject(pProcCtrlBlock);
 }
 
 
+
 //////////////////////////////////////////////////////////////////////////
 //
 // ÏÂÃæÊÇºÍ¿ØÖÆÌ¨ÃüÁî pm Ïà¹ØµÄ´úÂë¡£
 //
 
+/*
+Ìá¹©¸ÃÊ¾Àý´úÂëÊÇÎªÁË²ûÊÍÒ»¸ö¸ÅÄî£¬»òÕß½øÐÐÒ»¸ö²âÊÔ£¬²¢²»´ú±í×Å
+×î°²È«µÄ±àÂëÊµ¼ù£¬Òò´Ë²»Ó¦ÔÚÓ¦ÓÃ³ÌÐò»òÍøÕ¾ÖÐÊ¹ÓÃ¸ÃÊ¾Àý´úÂë¡£¶Ô
+ÓÚ³¬³ö±¾Ê¾Àý´úÂëµÄÔ¤ÆÚÓÃÍ¾ÒÔÍâµÄÊ¹ÓÃËùÔì³ÉµÄÅ¼È»»ò¼Ì·¢ÐÔËðÊ§£¬
+±±¾©Ó¢ÕæÊ±´ú¿Æ¼¼ÓÐÏÞ¹«Ë¾²»³Ðµ£ÈÎºÎÔðÈÎ¡£
+*/
+
+
 PRIVATE
 VOID
 ConsoleCmdPhysicalMemory(
 	IN HANDLE StdHandle
 	)
-/*++
-
-¹¦ÄÜÃèÊö£º
-	Í³¼ÆÊä³öÎïÀí´æ´¢Æ÷µÄÐÅÏ¢¡£¿ØÖÆÌ¨ÃüÁî¡°pm¡±¡£
-
-²ÎÊý£º
-	StdHandle -- ±ê×¼ÊäÈë¡¢Êä³ö¾ä±ú¡£
-
-·µ»ØÖµ£º
-	ÎÞ¡£
-
---*/
 {
 	BOOL IntState;
+	ULONG_PTR PfnArray[4];
 
 	IntState = KeEnableInterrupts(FALSE);	// ¹ØÖÐ¶Ï
 	
@@ -1094,10 +1217,34 @@ ConsoleCmdPhysicalMemory(
 	//
 	fprintf(StdHandle, "\nUsed Page Count: %d.\n", MiTotalPageFrameCount - MiZeroedPageCount - MiFreePageCount);
 	
+	
+	//////////////////////////////////////////////////////////////////////////
+	// ·ÖÅäÒ»¸öÎïÀíÒ³
+	//
+	MiAllocateAnyPages(4, PfnArray);
+	
+	fprintf(StdHandle, "\n****** After Allocate One Page ******\n");
+	fprintf(StdHandle, "Zeroed Page Count: %d.\n", MiZeroedPageCount);
+	fprintf(StdHandle, "Free Page Count: %d.\n", MiFreePageCount);
+	fprintf(StdHandle, "Used Page Count: %d.\n", MiTotalPageFrameCount - MiZeroedPageCount - MiFreePageCount);
+	
+	
+	//////////////////////////////////////////////////////////////////////////
+	//
+	// È»ºóÔÙÊÍ·ÅÕâ¸öÎïÀíÒ³
+	//
+	MiFreePages(4, PfnArray);
+	
+	fprintf(StdHandle, "\n****** After Free One Page ******\n");
+	fprintf(StdHandle, "Zeroed Page Count: %d.\n", MiZeroedPageCount);
+	fprintf(StdHandle, "Free Page Count: %d.\n", MiFreePageCount);
+	fprintf(StdHandle, "Used Page Count: %d.\n", MiTotalPageFrameCount - MiZeroedPageCount - MiFreePageCount);
+	
 	KeEnableInterrupts(IntState);	// ¿ªÖÐ¶Ï
 }
 
 
+
 //////////////////////////////////////////////////////////////////////////
 //
 // ÏÂÃæÊÇºÍ¿ØÖÆÌ¨ÃüÁî pt Ïà¹ØµÄ´úÂë¡£
-- 
2.34.1.windows.1


From 4d88b016b47e5f799018315560addc74daf43a57 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A7=9C=E6=B5=B7=E5=A4=A9?= <1076168468@qq.com>
Date: Sat, 29 Oct 2022 16:48:13 +0800
Subject: [PATCH 2/2] =?UTF-8?q?=E5=A4=84=E7=90=86MiAllocateAnyPages?=
 =?UTF-8?q?=E5=87=BD=E6=95=B0=E6=97=A0=E6=B3=95=E5=88=86=E9=85=8D=E5=86=85?=
 =?UTF-8?q?=E5=AD=98=E6=83=85=E5=86=B5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 pfnlist.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/pfnlist.c b/pfnlist.c
index 3a4ad74..19bf349 100644
--- a/pfnlist.c
+++ b/pfnlist.c
@@ -135,6 +135,7 @@ MiAllocateAnyPages(
 	BOOL IntState;
 	ULONG_PTR Pfn;
 	ULONG_PTR i;
+	STATUS status;
 
 	IntState = KeEnableInterrupts(FALSE);
 
@@ -167,11 +168,16 @@ MiAllocateAnyPages(
 
 			PfnArray[i] = Pfn;
 		}
+		
+		status = STATUS_SUCCESS;
+		
+	}else{
+		status = STATUS_NO_MEMORY;
 	}
 
 	KeEnableInterrupts(IntState);
 
-	return STATUS_SUCCESS;
+	return status;
 }
 
 STATUS
-- 
2.34.1.windows.1

