[KMTESTS:MM]
authorThomas Faber <thomas.faber@reactos.org>
Thu, 10 Sep 2015 11:16:53 +0000 (11:16 +0000)
committerThomas Faber <thomas.faber@reactos.org>
Thu, 10 Sep 2015 11:16:53 +0000 (11:16 +0000)
- Add some more reserved mapping tests for special pool tags and unaligned addresses
- Validate MDL pages in MmMdl test

svn path=/trunk/; revision=69169

rostests/kmtests/ntos_mm/MmMdl.c
rostests/kmtests/ntos_mm/MmReservedMapping.c

index 986a17a..7afd4a6 100644 (file)
@@ -19,6 +19,8 @@ TestMmAllocatePagesForMdl(VOID)
     PMDL Mdls[32];
     PVOID SystemVas[32];
     ULONG i;
     PMDL Mdls[32];
     PVOID SystemVas[32];
     ULONG i;
+    PPFN_NUMBER MdlPages;
+    ULONG MdlPageCount;
 
     LowAddress.QuadPart = 0;
     HighAddress.QuadPart = -1;
 
     LowAddress.QuadPart = 0;
     HighAddress.QuadPart = -1;
@@ -34,6 +36,14 @@ TestMmAllocatePagesForMdl(VOID)
     ok(MmGetMdlByteCount(Mdl) == 2 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
     ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdl));
     ok(!(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
     ok(MmGetMdlByteCount(Mdl) == 2 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
     ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdl));
     ok(!(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
+    MdlPages = MmGetMdlPfnArray(Mdl);
+    MdlPageCount = ADDRESS_AND_SIZE_TO_SPAN_PAGES(MmGetMdlVirtualAddress(Mdl), MmGetMdlByteCount(Mdl));
+    ok(MdlPageCount == 2 * 1024 * 1024 / PAGE_SIZE, "MdlPageCount = %lu\n", MdlPageCount);
+    for (i = 0; i < MdlPageCount; i++)
+    {
+        ok(MdlPages[i] != 0 && MdlPages[i] != (PFN_NUMBER)-1,
+           "MdlPages[%lu] = 0x%I64x\n", i, (ULONGLONG)MdlPages[i]);
+    }
     MmFreePagesFromMdl(Mdl);
     ExFreePoolWithTag(Mdl, 0);
 
     MmFreePagesFromMdl(Mdl);
     ExFreePoolWithTag(Mdl, 0);
 
@@ -105,6 +115,14 @@ TestMmAllocatePagesForMdl(VOID)
         ok(MmGetMdlByteCount(Mdl) != 2UL * 1024 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
         ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdl));
         ok(!(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
         ok(MmGetMdlByteCount(Mdl) != 2UL * 1024 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
         ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdl));
         ok(!(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
+        MdlPages = MmGetMdlPfnArray(Mdl);
+        MdlPageCount = ADDRESS_AND_SIZE_TO_SPAN_PAGES(MmGetMdlVirtualAddress(Mdl), MmGetMdlByteCount(Mdl));
+        ok(MdlPageCount < 2UL * 1024 * 1024 * 1024 / PAGE_SIZE, "MdlPageCount = %lu\n", MdlPageCount);
+        for (i = 0; i < MdlPageCount; i++)
+        {
+            ok(MdlPages[i] != 0 && MdlPages[i] != (PFN_NUMBER)-1,
+               "MdlPages[%lu] = 0x%I64x\n", i, (ULONGLONG)MdlPages[i]);
+        }
         SystemVa = MmMapLockedPagesSpecifyCache(Mdl,
                                                 KernelMode,
                                                 MmCached,
         SystemVa = MmMapLockedPagesSpecifyCache(Mdl,
                                                 KernelMode,
                                                 MmCached,
index 2e437f2..8a0dbdb 100644 (file)
@@ -35,6 +35,7 @@ BOOLEAN
 ValidateMapping(
     _In_ PVOID BaseAddress,
     _In_ ULONG TotalPtes,
 ValidateMapping(
     _In_ PVOID BaseAddress,
     _In_ ULONG TotalPtes,
+    _In_ ULONG PoolTag,
     _In_ ULONG ValidPtes,
     _In_ PPFN_NUMBER Pfns)
 {
     _In_ ULONG ValidPtes,
     _In_ PPFN_NUMBER Pfns)
 {
@@ -61,7 +62,7 @@ ValidateMapping(
                 ok_eq_hex(PointerPte[i].u.Long, 0UL);
     }
     Valid = Valid &&
                 ok_eq_hex(PointerPte[i].u.Long, 0UL);
     }
     Valid = Valid &&
-            ok_eq_tag(PointerPte[-1].u.Long, 'MRmK' & ~1);
+            ok_eq_tag(PointerPte[-1].u.Long, PoolTag & ~1);
     Valid = Valid &&
             ok_eq_ulong(PointerPte[-2].u.Long, (TotalPtes + 2) * 2);
 #endif
     Valid = Valid &&
             ok_eq_ulong(PointerPte[-2].u.Long, (TotalPtes + 2) * 2);
 #endif
@@ -72,7 +73,9 @@ ValidateMapping(
 static
 VOID
 TestMap(
 static
 VOID
 TestMap(
-    _In_ PVOID Mapping)
+    _In_ PVOID Mapping,
+    _In_ ULONG TotalPtes,
+    _In_ ULONG PoolTag)
 {
     PMDL Mdl;
     PHYSICAL_ADDRESS ZeroPhysical;
 {
     PMDL Mdl;
     PHYSICAL_ADDRESS ZeroPhysical;
@@ -99,14 +102,40 @@ TestMap(
     MdlPages = (PVOID)(Mdl + 1);
 
     BaseAddress = MmMapLockedPagesWithReservedMapping(Mapping,
     MdlPages = (PVOID)(Mdl + 1);
 
     BaseAddress = MmMapLockedPagesWithReservedMapping(Mapping,
-                                                      'MRmK',
+                                                      PoolTag,
                                                       Mdl,
                                                       MmCached);
                                                       Mdl,
                                                       MmCached);
-    if (BaseAddress)
+    ok(BaseAddress != NULL, "MmMapLockedPagesWithReservedMapping failed\n");
+    if (!skip(BaseAddress != NULL, "Failed to map MDL\n"))
     {
         ok_eq_pointer(BaseAddress, Mapping);
 
     {
         ok_eq_pointer(BaseAddress, Mapping);
 
-        ok_bool_true(ValidateMapping(BaseAddress, 10, 1, MdlPages),
+        ok_bool_true(ValidateMapping(BaseAddress, TotalPtes, PoolTag, 1, MdlPages),
+                     "ValidateMapping returned");
+
+        KmtStartSeh()
+            *(volatile ULONG *)BaseAddress = 0x01234567;
+        KmtEndSeh(STATUS_SUCCESS);
+
+        MmUnmapReservedMapping(BaseAddress,
+                               PoolTag,
+                               Mdl);
+
+        ok_bool_true(ValidateMapping(Mapping, TotalPtes, PoolTag, 0, NULL),
+                     "ValidateMapping returned");
+    }
+
+    /* Try again but at an unaligned address */
+    BaseAddress = MmMapLockedPagesWithReservedMapping((PUCHAR)Mapping + sizeof(ULONG),
+                                                      PoolTag,
+                                                      Mdl,
+                                                      MmCached);
+    ok(BaseAddress != NULL, "MmMapLockedPagesWithReservedMapping failed\n");
+    if (!skip(BaseAddress != NULL, "Failed to map MDL\n"))
+    {
+        ok_eq_pointer(BaseAddress, (PUCHAR)Mapping + sizeof(ULONG));
+
+        ok_bool_true(ValidateMapping(BaseAddress, TotalPtes, PoolTag, 1, MdlPages),
                      "ValidateMapping returned");
 
         KmtStartSeh()
                      "ValidateMapping returned");
 
         KmtStartSeh()
@@ -114,10 +143,10 @@ TestMap(
         KmtEndSeh(STATUS_SUCCESS);
 
         MmUnmapReservedMapping(BaseAddress,
         KmtEndSeh(STATUS_SUCCESS);
 
         MmUnmapReservedMapping(BaseAddress,
-                               'MRmK',
+                               PoolTag,
                                Mdl);
 
                                Mdl);
 
-        ok_bool_true(ValidateMapping(Mapping, 10, 0, NULL),
+        ok_bool_true(ValidateMapping(Mapping, TotalPtes, PoolTag, 0, NULL),
                      "ValidateMapping returned");
     }
 
                      "ValidateMapping returned");
     }
 
@@ -127,7 +156,7 @@ TestMap(
     Mdl = MmAllocatePagesForMdlEx(ZeroPhysical,
                                   MaxPhysical,
                                   ZeroPhysical,
     Mdl = MmAllocatePagesForMdlEx(ZeroPhysical,
                                   MaxPhysical,
                                   ZeroPhysical,
-                                  10 * PAGE_SIZE,
+                                  TotalPtes * PAGE_SIZE,
                                   MmCached,
                                   0);
     if (skip(Mdl != NULL, "No MDL\n"))
                                   MmCached,
                                   0);
     if (skip(Mdl != NULL, "No MDL\n"))
@@ -138,17 +167,18 @@ TestMap(
     MdlPages = (PVOID)(Mdl + 1);
 
     BaseAddress = MmMapLockedPagesWithReservedMapping(Mapping,
     MdlPages = (PVOID)(Mdl + 1);
 
     BaseAddress = MmMapLockedPagesWithReservedMapping(Mapping,
-                                                      'MRmK',
+                                                      PoolTag,
                                                       Mdl,
                                                       MmCached);
                                                       Mdl,
                                                       MmCached);
-    if (BaseAddress)
+    ok(BaseAddress != NULL, "MmMapLockedPagesWithReservedMapping failed\n");
+    if (!skip(BaseAddress != NULL, "Failed to map MDL\n"))
     {
         ok_eq_pointer(BaseAddress, Mapping);
 
     {
         ok_eq_pointer(BaseAddress, Mapping);
 
-        ok_bool_true(ValidateMapping(BaseAddress, 10, 10, MdlPages),
+        ok_bool_true(ValidateMapping(BaseAddress, TotalPtes, PoolTag, TotalPtes, MdlPages),
                      "ValidateMapping returned");
 
                      "ValidateMapping returned");
 
-        for (i = 0; i < 10; i++)
+        for (i = 0; i < TotalPtes; i++)
         {
             KmtStartSeh()
                 *((volatile ULONG *)BaseAddress + i * PAGE_SIZE / sizeof(ULONG)) = 0x01234567;
         {
             KmtStartSeh()
                 *((volatile ULONG *)BaseAddress + i * PAGE_SIZE / sizeof(ULONG)) = 0x01234567;
@@ -156,10 +186,10 @@ TestMap(
         }
 
         MmUnmapReservedMapping(BaseAddress,
         }
 
         MmUnmapReservedMapping(BaseAddress,
-                               'MRmK',
+                               PoolTag,
                                Mdl);
 
                                Mdl);
 
-        ok_bool_true(ValidateMapping(Mapping, 10, 0, NULL),
+        ok_bool_true(ValidateMapping(Mapping, TotalPtes, PoolTag, 0, NULL),
                      "ValidateMapping returned");
     }
 
                      "ValidateMapping returned");
     }
 
@@ -169,7 +199,7 @@ TestMap(
     Mdl = MmAllocatePagesForMdlEx(ZeroPhysical,
                                   MaxPhysical,
                                   ZeroPhysical,
     Mdl = MmAllocatePagesForMdlEx(ZeroPhysical,
                                   MaxPhysical,
                                   ZeroPhysical,
-                                  11 * PAGE_SIZE,
+                                  (TotalPtes + 1) * PAGE_SIZE,
                                   MmCached,
                                   0);
     if (skip(Mdl != NULL, "No MDL\n"))
                                   MmCached,
                                   0);
     if (skip(Mdl != NULL, "No MDL\n"))
@@ -178,14 +208,14 @@ TestMap(
     }
 
     BaseAddress = MmMapLockedPagesWithReservedMapping(Mapping,
     }
 
     BaseAddress = MmMapLockedPagesWithReservedMapping(Mapping,
-                                                      'MRmK',
+                                                      PoolTag,
                                                       Mdl,
                                                       MmCached);
     ok_eq_pointer(BaseAddress, NULL);
     if (BaseAddress)
     {
         MmUnmapReservedMapping(BaseAddress,
                                                       Mdl,
                                                       MmCached);
     ok_eq_pointer(BaseAddress, NULL);
     if (BaseAddress)
     {
         MmUnmapReservedMapping(BaseAddress,
-                               'MRmK',
+                               PoolTag,
                                Mdl);
     }
 
                                Mdl);
     }
 
@@ -201,7 +231,7 @@ START_TEST(MmReservedMapping)
     ok(Mapping != NULL, "MmAllocateMappingAddress failed\n");
     if (!skip(Mapping != NULL, "No mapping\n"))
     {
     ok(Mapping != NULL, "MmAllocateMappingAddress failed\n");
     if (!skip(Mapping != NULL, "No mapping\n"))
     {
-        ok_bool_true(ValidateMapping(Mapping, 1, 0, NULL),
+        ok_bool_true(ValidateMapping(Mapping, 1, 'MRmK', 0, NULL),
                      "ValidateMapping returned");
 
         MmFreeMappingAddress(Mapping, 'MRmK');
                      "ValidateMapping returned");
 
         MmFreeMappingAddress(Mapping, 'MRmK');
@@ -212,7 +242,7 @@ START_TEST(MmReservedMapping)
     ok(Mapping != NULL, "MmAllocateMappingAddress failed\n");
     if (!skip(Mapping != NULL, "No mapping\n"))
     {
     ok(Mapping != NULL, "MmAllocateMappingAddress failed\n");
     if (!skip(Mapping != NULL, "No mapping\n"))
     {
-        ok_bool_true(ValidateMapping(Mapping, 10, 0, NULL),
+        ok_bool_true(ValidateMapping(Mapping, 10, 'MRmK', 0, NULL),
                      "ValidateMapping returned");
 
         /* PAGE_FAULT_IN_NONPAGED_AREA can't be caught with SEH */
                      "ValidateMapping returned");
 
         /* PAGE_FAULT_IN_NONPAGED_AREA can't be caught with SEH */
@@ -221,8 +251,42 @@ START_TEST(MmReservedMapping)
             (void)*(volatile UCHAR *)Mapping;
         }
 
             (void)*(volatile UCHAR *)Mapping;
         }
 
-        TestMap(Mapping);
+        TestMap(Mapping, 10, 'MRmK');
 
         MmFreeMappingAddress(Mapping, 'MRmK');
     }
 
         MmFreeMappingAddress(Mapping, 'MRmK');
     }
+
+    /* PoolTag = 0 */
+    Mapping = MmAllocateMappingAddress(1, 0);
+    ok(Mapping == NULL, "MmAllocateMappingAddress failed\n");
+    if (Mapping != NULL)
+    {
+        MmFreeMappingAddress(Mapping, 0);
+    }
+
+    /* PoolTag = 1 */
+    Mapping = MmAllocateMappingAddress(1, 1);
+    ok(Mapping != NULL, "MmAllocateMappingAddress failed\n");
+    if (Mapping != NULL)
+    {
+        ok_bool_true(ValidateMapping(Mapping, 1, 1, 0, NULL),
+                     "ValidateMapping returned");
+
+        TestMap(Mapping, 1, 1);
+
+        MmFreeMappingAddress(Mapping, 1);
+    }
+
+    /* Free an unaligned address */
+    Mapping = MmAllocateMappingAddress(PAGE_SIZE, 'MRmK');
+    ok(Mapping != NULL, "MmAllocateMappingAddress failed\n");
+    if (Mapping != NULL)
+    {
+        ok_bool_true(ValidateMapping(Mapping, 1, 'MRmK', 0, NULL),
+                     "ValidateMapping returned");
+
+        TestMap(Mapping, 1, 'MRmK');
+
+        MmFreeMappingAddress((PUCHAR)Mapping + sizeof(ULONG), 'MRmK');
+    }
 }
 }