85dd3fc501dbca50b25b167a7e3ebfbf249d0a35
[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
39 /* Now map/unmap it */
40 Mdl = MmAllocatePagesForMdl(LowAddress,
41 HighAddress,
42 SkipBytes,
43 2 * 1024 * 1024);
44 ok(Mdl != NULL, "MmAllocatePagesForMdl failed\n");
45 if (skip(Mdl != NULL, "No Mdl\n"))
46 return;
47 ok(MmGetMdlByteCount(Mdl) == 2 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
48 ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdl));
49 ok(!(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
50 SystemVa = MmMapLockedPagesSpecifyCache(Mdl,
51 KernelMode,
52 MmCached,
53 NULL,
54 FALSE,
55 NormalPagePriority);
56 ok(SystemVa != NULL, "MmMapLockedPagesSpecifyCache failed\n");
57 if (!skip(SystemVa != NULL, "No system VA\n"))
58 {
59 ok(MmGetMdlByteCount(Mdl) == 2 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
60 ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p, System VA: %p\n", MmGetMdlVirtualAddress(Mdl), SystemVa);
61 ok(Mdl->MappedSystemVa == SystemVa, "MappedSystemVa: %p, System VA: %p\n", Mdl->MappedSystemVa, SystemVa);
62 ok((Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
63 MmUnmapLockedPages(SystemVa, Mdl);
64 }
65 ok(MmGetMdlByteCount(Mdl) == 2 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
66 ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdl));
67 ok(!(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
68 MmFreePagesFromMdl(Mdl);
69
70 /* Now map it, and free without unmapping */
71 Mdl = MmAllocatePagesForMdl(LowAddress,
72 HighAddress,
73 SkipBytes,
74 2 * 1024 * 1024);
75 ok(Mdl != NULL, "MmAllocatePagesForMdl failed\n");
76 if (skip(Mdl != NULL, "No Mdl\n"))
77 return;
78 ok(MmGetMdlByteCount(Mdl) == 2 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
79 ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdl));
80 ok(!(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
81 SystemVa = MmMapLockedPagesSpecifyCache(Mdl,
82 KernelMode,
83 MmCached,
84 NULL,
85 FALSE,
86 NormalPagePriority);
87 ok(SystemVa != NULL, "MmMapLockedPagesSpecifyCache failed\n");
88 ok(MmGetMdlByteCount(Mdl) == 2 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
89 ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p, System VA: %p\n", MmGetMdlVirtualAddress(Mdl), SystemVa);
90 ok(Mdl->MappedSystemVa == SystemVa, "MappedSystemVa: %p, System VA: %p\n", Mdl->MappedSystemVa, SystemVa);
91 ok((Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
92 MmFreePagesFromMdl(Mdl);
93
94 /* try to allocate 2 GB -- should succeed but not map */
95 Mdl = MmAllocatePagesForMdl(LowAddress,
96 HighAddress,
97 SkipBytes,
98 2UL * 1024 * 1024 * 1024);
99 ok(Mdl != NULL, "MmAllocatePagesForMdl failed for 2 GB\n");
100 if (Mdl != NULL)
101 {
102 ok(MmGetMdlByteCount(Mdl) != 2UL * 1024 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
103 ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdl));
104 ok(!(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
105 SystemVa = MmMapLockedPagesSpecifyCache(Mdl,
106 KernelMode,
107 MmCached,
108 NULL,
109 FALSE,
110 NormalPagePriority);
111 ok(SystemVa == NULL, "MmMapLockedPagesSpecifyCache succeeded for 2 GB\n");
112 if (SystemVa != NULL)
113 MmUnmapLockedPages(SystemVa, Mdl);
114 ok(MmGetMdlByteCount(Mdl) != 2UL * 1024 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdl));
115 ok(MmGetMdlVirtualAddress(Mdl) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdl));
116 ok(!(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdl->MdlFlags);
117 MmFreePagesFromMdl(Mdl);
118 }
119
120 /* now allocate and map 32 MB Mdls until we fail */
121 for (i = 0; i < sizeof(Mdls) / sizeof(Mdls[0]); i++)
122 {
123 Mdls[i] = MmAllocatePagesForMdl(LowAddress,
124 HighAddress,
125 SkipBytes,
126 32 * 1024 * 1024);
127 if (Mdls[i] == NULL)
128 {
129 trace("MmAllocatePagesForMdl failed with i = %lu\n", i);
130 break;
131 }
132 ok(MmGetMdlVirtualAddress(Mdls[i]) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdls[i]));
133 ok(!(Mdls[i]->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdls[i]->MdlFlags);
134 SystemVas[i] = MmMapLockedPagesSpecifyCache(Mdls[i],
135 KernelMode,
136 MmCached,
137 NULL,
138 FALSE,
139 NormalPagePriority);
140 if (SystemVas[i] == NULL)
141 {
142 ok(MmGetMdlByteCount(Mdls[i]) <= 32 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdls[i]));
143 ok(MmGetMdlVirtualAddress(Mdls[i]) == NULL, "Virtual address: %p\n", MmGetMdlVirtualAddress(Mdls[i]));
144 ok(!(Mdls[i]->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdls[i]->MdlFlags);
145 trace("MmMapLockedPagesSpecifyCache failed with i = %lu\n", i);
146 break;
147 }
148 ok(MmGetMdlByteCount(Mdls[i]) == 32 * 1024 * 1024, "Byte count: %lu\n", MmGetMdlByteCount(Mdls[i]));
149 ok(MmGetMdlVirtualAddress(Mdls[i]) == NULL, "Virtual address: %p, System VA: %p\n", MmGetMdlVirtualAddress(Mdls[i]), SystemVas[i]);
150 ok(Mdls[i]->MappedSystemVa == SystemVas[i], "MappedSystemVa: %p\n", Mdls[i]->MappedSystemVa, SystemVas[i]);
151 ok((Mdls[i]->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA), "MdlFlags: %lx\n", Mdls[i]->MdlFlags);
152 }
153 for (i = 0; i < sizeof(Mdls) / sizeof(Mdls[0]); i++)
154 {
155 if (Mdls[i] == NULL)
156 break;
157 if (SystemVas[i] != NULL)
158 MmUnmapLockedPages(SystemVas[i], Mdls[i]);
159 MmFreePagesFromMdl(Mdls[i]);
160 if (SystemVas[i] == NULL)
161 break;
162 }
163 }
164
165 START_TEST(MmMdl)
166 {
167 TestMmAllocatePagesForMdl();
168 }