[USP10_WINETEST] Sync with Wine Staging 2.9. CORE-13362
[reactos.git] / rostests / kmtests / ntos_mm / MmReservedMapping.c
index 2e437f2..fd35a20 100644 (file)
@@ -30,11 +30,25 @@ _MiAddressToPte(PVOID Address)
 
 #endif
 
+static
+_Must_inspect_result_
+_IRQL_requires_max_ (DISPATCH_LEVEL)
+PMDL
+(NTAPI
+*pMmAllocatePagesForMdlEx)(
+    _In_ PHYSICAL_ADDRESS LowAddress,
+    _In_ PHYSICAL_ADDRESS HighAddress,
+    _In_ PHYSICAL_ADDRESS SkipBytes,
+    _In_ SIZE_T TotalBytes,
+    _In_ MEMORY_CACHING_TYPE CacheType,
+    _In_ ULONG Flags);
+
 static
 BOOLEAN
 ValidateMapping(
     _In_ PVOID BaseAddress,
     _In_ ULONG TotalPtes,
+    _In_ ULONG PoolTag,
     _In_ ULONG ValidPtes,
     _In_ PPFN_NUMBER Pfns)
 {
@@ -61,7 +75,7 @@ ValidateMapping(
                 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
@@ -72,7 +86,9 @@ ValidateMapping(
 static
 VOID
 TestMap(
-    _In_ PVOID Mapping)
+    _In_ PVOID Mapping,
+    _In_ ULONG TotalPtes,
+    _In_ ULONG PoolTag)
 {
     PMDL Mdl;
     PHYSICAL_ADDRESS ZeroPhysical;
@@ -81,16 +97,21 @@ TestMap(
     PPFN_NUMBER MdlPages;
     ULONG i;
 
+    if (skip(pMmAllocatePagesForMdlEx != NULL, "MmAllocatePagesForMdlEx unavailable\n"))
+    {
+        return;
+    }
+
     ZeroPhysical.QuadPart = 0;
     MaxPhysical.QuadPart = 0xffffffffffffffffLL;
 
     /* Create a one-page MDL and map it */
-    Mdl = MmAllocatePagesForMdlEx(ZeroPhysical,
-                                  MaxPhysical,
-                                  ZeroPhysical,
-                                  PAGE_SIZE,
-                                  MmCached,
-                                  0);
+    Mdl = pMmAllocatePagesForMdlEx(ZeroPhysical,
+                                   MaxPhysical,
+                                   ZeroPhysical,
+                                   PAGE_SIZE,
+                                   MmCached,
+                                   0);
     if (skip(Mdl != NULL, "No MDL\n"))
     {
         return;
@@ -99,14 +120,15 @@ TestMap(
     MdlPages = (PVOID)(Mdl + 1);
 
     BaseAddress = MmMapLockedPagesWithReservedMapping(Mapping,
-                                                      'MRmK',
+                                                      PoolTag,
                                                       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_bool_true(ValidateMapping(BaseAddress, 10, 1, MdlPages),
+        ok_bool_true(ValidateMapping(BaseAddress, TotalPtes, PoolTag, 1, MdlPages),
                      "ValidateMapping returned");
 
         KmtStartSeh()
@@ -114,22 +136,47 @@ TestMap(
         KmtEndSeh(STATUS_SUCCESS);
 
         MmUnmapReservedMapping(BaseAddress,
-                               'MRmK',
+                               PoolTag,
                                Mdl);
 
-        ok_bool_true(ValidateMapping(Mapping, 10, 0, NULL),
+        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()
+            *(volatile ULONG *)BaseAddress = 0x01234567;
+        KmtEndSeh(STATUS_SUCCESS);
+
+        MmUnmapReservedMapping(BaseAddress,
+                               PoolTag,
+                               Mdl);
+
+        ok_bool_true(ValidateMapping(Mapping, TotalPtes, PoolTag, 0, NULL),
                      "ValidateMapping returned");
     }
 
     MmFreePagesFromMdl(Mdl);
 
     /* Map all pages */
-    Mdl = MmAllocatePagesForMdlEx(ZeroPhysical,
-                                  MaxPhysical,
-                                  ZeroPhysical,
-                                  10 * PAGE_SIZE,
-                                  MmCached,
-                                  0);
+    Mdl = pMmAllocatePagesForMdlEx(ZeroPhysical,
+                                   MaxPhysical,
+                                   ZeroPhysical,
+                                   TotalPtes * PAGE_SIZE,
+                                   MmCached,
+                                   0);
     if (skip(Mdl != NULL, "No MDL\n"))
     {
         return;
@@ -138,17 +185,18 @@ TestMap(
     MdlPages = (PVOID)(Mdl + 1);
 
     BaseAddress = MmMapLockedPagesWithReservedMapping(Mapping,
-                                                      'MRmK',
+                                                      PoolTag,
                                                       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_bool_true(ValidateMapping(BaseAddress, 10, 10, MdlPages),
+        ok_bool_true(ValidateMapping(BaseAddress, TotalPtes, PoolTag, TotalPtes, MdlPages),
                      "ValidateMapping returned");
 
-        for (i = 0; i < 10; i++)
+        for (i = 0; i < TotalPtes; i++)
         {
             KmtStartSeh()
                 *((volatile ULONG *)BaseAddress + i * PAGE_SIZE / sizeof(ULONG)) = 0x01234567;
@@ -156,36 +204,36 @@ TestMap(
         }
 
         MmUnmapReservedMapping(BaseAddress,
-                               'MRmK',
+                               PoolTag,
                                Mdl);
 
-        ok_bool_true(ValidateMapping(Mapping, 10, 0, NULL),
+        ok_bool_true(ValidateMapping(Mapping, TotalPtes, PoolTag, 0, NULL),
                      "ValidateMapping returned");
     }
 
     MmFreePagesFromMdl(Mdl);
 
     /* Try to map more pages than we reserved */
-    Mdl = MmAllocatePagesForMdlEx(ZeroPhysical,
-                                  MaxPhysical,
-                                  ZeroPhysical,
-                                  11 * PAGE_SIZE,
-                                  MmCached,
-                                  0);
+    Mdl = pMmAllocatePagesForMdlEx(ZeroPhysical,
+                                   MaxPhysical,
+                                   ZeroPhysical,
+                                   (TotalPtes + 1) * PAGE_SIZE,
+                                   MmCached,
+                                   0);
     if (skip(Mdl != NULL, "No MDL\n"))
     {
         return;
     }
 
     BaseAddress = MmMapLockedPagesWithReservedMapping(Mapping,
-                                                      'MRmK',
+                                                      PoolTag,
                                                       Mdl,
                                                       MmCached);
     ok_eq_pointer(BaseAddress, NULL);
     if (BaseAddress)
     {
         MmUnmapReservedMapping(BaseAddress,
-                               'MRmK',
+                               PoolTag,
                                Mdl);
     }
 
@@ -196,12 +244,14 @@ START_TEST(MmReservedMapping)
 {
     PVOID Mapping;
 
+    pMmAllocatePagesForMdlEx = KmtGetSystemRoutineAddress(L"MmAllocatePagesForMdlEx");
+
     /* one byte - single page */
     Mapping = MmAllocateMappingAddress(1, 'MRmK');
     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');
@@ -212,7 +262,7 @@ START_TEST(MmReservedMapping)
     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 */
@@ -221,8 +271,42 @@ START_TEST(MmReservedMapping)
             (void)*(volatile UCHAR *)Mapping;
         }
 
-        TestMap(Mapping);
+        TestMap(Mapping, 10, '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');
+    }
 }