986a17a6023929c40109f0907e6b73637e69514c
[reactos.git] / rostests / kmtests / ntos_mm / MmMdl.c
1 /*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Kernel-Mode Test Suite MDL test
5 * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6 */
7
8 #include <kmt_test.h>
9
10 static
11 VOID
12 TestMmAllocatePagesForMdl(VOID)
13 {
14 PMDL Mdl;
15 PHYSICAL_ADDRESS LowAddress;
16 PHYSICAL_ADDRESS HighAddress;
17 PHYSICAL_ADDRESS SkipBytes;
18 PVOID SystemVa;
19 PMDL Mdls[32];
20 PVOID SystemVas[32];
21 ULONG i;
22
23 LowAddress.QuadPart = 0;
24 HighAddress.QuadPart = -1;
25 SkipBytes.QuadPart = 0;
26 /* simple allocate/free */
27 Mdl = MmAllocatePagesForMdl(LowAddress,
28 HighAddress,
29 SkipBytes,
30 2 * 1024 * 1024);
31 ok(Mdl != NULL, "MmAllocatePagesForMdl failed\n");
32 if (skip(Mdl != NULL, "No Mdl\n"))
33 return;
34 ok(MmGetMdlByteCount(Mdl) == 2 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
35 ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdl));
36 ok(!(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
37 MmFreePagesFromMdl(Mdl);
38 ExFreePoolWithTag(Mdl, 0);
39
40 /* Now map/unmap it */
41 Mdl = MmAllocatePagesForMdl(LowAddress,
42 HighAddress,
43 SkipBytes,
44 2 * 1024 * 1024);
45 ok(Mdl != NULL, "MmAllocatePagesForMdl failed\n");
46 if (skip(Mdl != NULL, "No Mdl\n"))
47 return;
48 ok(MmGetMdlByteCount(Mdl) == 2 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
49 ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdl));
50 ok(!(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
51 SystemVa = MmMapLockedPagesSpecifyCache(Mdl,
52 KernelMode,
53 MmCached,
54 NULL,
55 FALSE,
56 NormalPagePriority);
57 ok(SystemVa != NULL, "MmMapLockedPagesSpecifyCache failed\n");
58 if (!skip(SystemVa != NULL, "No system VA\n"))
59 {
60 ok(MmGetMdlByteCount(Mdl) == 2 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
61 ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p, System VA: %p\n", MmGetMdlVirtualAddress(Mdl), SystemVa);
62 ok(Mdl->MappedSystemVa == SystemVa, "MappedSystemVa: %p, System VA: %p\n", Mdl->MappedSystemVa, SystemVa);
63 ok((Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
64 MmUnmapLockedPages(SystemVa, Mdl);
65 }
66 ok(MmGetMdlByteCount(Mdl) == 2 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
67 ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdl));
68 ok(!(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
69 MmFreePagesFromMdl(Mdl);
70 ExFreePoolWithTag(Mdl, 0);
71
72 /* Now map it, and free without unmapping */
73 Mdl = MmAllocatePagesForMdl(LowAddress,
74 HighAddress,
75 SkipBytes,
76 2 * 1024 * 1024);
77 ok(Mdl != NULL, "MmAllocatePagesForMdl failed\n");
78 if (skip(Mdl != NULL, "No Mdl\n"))
79 return;
80 ok(MmGetMdlByteCount(Mdl) == 2 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
81 ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdl));
82 ok(!(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
83 SystemVa = MmMapLockedPagesSpecifyCache(Mdl,
84 KernelMode,
85 MmCached,
86 NULL,
87 FALSE,
88 NormalPagePriority);
89 ok(SystemVa != NULL, "MmMapLockedPagesSpecifyCache failed\n");
90 ok(MmGetMdlByteCount(Mdl) == 2 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
91 ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p, System VA: %p\n", MmGetMdlVirtualAddress(Mdl), SystemVa);
92 ok(Mdl->MappedSystemVa == SystemVa, "MappedSystemVa: %p, System VA: %p\n", Mdl->MappedSystemVa, SystemVa);
93 ok((Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
94 MmFreePagesFromMdl(Mdl);
95 ExFreePoolWithTag(Mdl, 0);
96
97 /* try to allocate 2 GB -- should succeed but not map */
98 Mdl = MmAllocatePagesForMdl(LowAddress,
99 HighAddress,
100 SkipBytes,
101 2UL * 1024 * 1024 * 1024);
102 ok(Mdl != NULL, "MmAllocatePagesForMdl failed for 2 GB\n");
103 if (Mdl != NULL)
104 {
105 ok(MmGetMdlByteCount(Mdl) != 2UL * 1024 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
106 ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdl));
107 ok(!(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
108 SystemVa = MmMapLockedPagesSpecifyCache(Mdl,
109 KernelMode,
110 MmCached,
111 NULL,
112 FALSE,
113 NormalPagePriority);
114 ok(SystemVa == NULL, "MmMapLockedPagesSpecifyCache succeeded for 2 GB\n");
115 if (SystemVa != NULL)
116 MmUnmapLockedPages(SystemVa, Mdl);
117 ok(MmGetMdlByteCount(Mdl) != 2UL * 1024 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
118 ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdl));
119 ok(!(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
120 MmFreePagesFromMdl(Mdl);
121 ExFreePoolWithTag(Mdl, 0);
122 }
123
124 /* now allocate and map 32 MB Mdls until we fail */
125 for (i = 0; i < sizeof(Mdls) / sizeof(Mdls[0]); i++)
126 {
127 Mdls[i] = MmAllocatePagesForMdl(LowAddress,
128 HighAddress,
129 SkipBytes,
130 32 * 1024 * 1024);
131 if (Mdls[i] == NULL)
132 {
133 trace("MmAllocatePagesForMdl failed with i = %lu\n", i);
134 break;
135 }
136 ok(MmGetMdlVirtualAddress(Mdls[i]) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdls[i]));
137 ok(!(Mdls[i]->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdls[i]->MdlFlags);
138 SystemVas[i] = MmMapLockedPagesSpecifyCache(Mdls[i],
139 KernelMode,
140 MmCached,
141 NULL,
142 FALSE,
143 NormalPagePriority);
144 if (SystemVas[i] == NULL)
145 {
146 ok(MmGetMdlByteCount(Mdls[i]) <= 32 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdls[i]));
147 ok(MmGetMdlVirtualAddress(Mdls[i]) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdls[i]));
148 ok(!(Mdls[i]->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdls[i]->MdlFlags);
149 trace("MmMapLockedPagesSpecifyCache failed with i = %lu\n", i);
150 break;
151 }
152 ok(MmGetMdlByteCount(Mdls[i]) == 32 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdls[i]));
153 ok(MmGetMdlVirtualAddress(Mdls[i]) == NULL, "Virtual address: %p, System VA: %p\n", MmGetMdlVirtualAddress(Mdls[i]), SystemVas[i]);
154 ok(Mdls[i]->MappedSystemVa == SystemVas[i], "MappedSystemVa: %p\n", Mdls[i]->MappedSystemVa, SystemVas[i]);
155 ok((Mdls[i]->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdls[i]->MdlFlags);
156 }
157 for (i = 0; i < sizeof(Mdls) / sizeof(Mdls[0]); i++)
158 {
159 if (Mdls[i] == NULL)
160 break;
161 if (SystemVas[i] != NULL)
162 MmUnmapLockedPages(SystemVas[i], Mdls[i]);
163 MmFreePagesFromMdl(Mdls[i]);
164 ExFreePoolWithTag(Mdls[i], 0);
165 if (SystemVas[i] == NULL)
166 break;
167 }
168 }
169
170 START_TEST(MmMdl)
171 {
172 TestMmAllocatePagesForMdl();
173 }