ReactOS 0.4.16-dev-87-g3dfbe52
MmMapLockedPagesSpecifyCache_drv.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Test driver for MmMapLockedPagesSpecifyCache function
5 * PROGRAMMER: Pierre Schweitzer <pierre@reactos.org>
6 */
7
8#include <kmt_test.h>
9
10#define NDEBUG
11#include <debug.h>
12
14
15static KMT_IRP_HANDLER TestIrpHandler;
16static KMT_MESSAGE_HANDLER TestMessageHandler;
17
22
28 IN OUT INT *Flags)
29{
31
32 PAGED_CODE();
33
36
37 *DeviceName = L"MmMapLockedPagesSpecifyCache";
38
41
42 return Status;
43}
44
45VOID
48{
49 PAGED_CODE();
50}
51
52VOID
54{
55 NTSTATUS SehStatus;
56
57 if (CurrentMdl == NULL)
58 {
59 return;
60 }
61
62 if (CurrentUser != NULL)
63 {
64 SehStatus = STATUS_SUCCESS;
66 {
68 }
70 {
71 SehStatus = _SEH2_GetExceptionCode();
72 }
74 ok_eq_hex(SehStatus, STATUS_SUCCESS);
76 }
77
78 SehStatus = STATUS_SUCCESS;
80 {
82 }
84 {
85 SehStatus = _SEH2_GetExceptionCode();
86 }
88 ok_eq_hex(SehStatus, STATUS_SUCCESS);
91 {
93 }
94 else
95 {
97 }
99}
100
101static
107 IN SIZE_T InLength,
108 IN OUT PSIZE_T OutLength)
109{
111 NTSTATUS SehStatus;
112
113 switch (ControlCode)
114 {
116 {
117 ok(Buffer != NULL, "Buffer is NULL\n");
118 ok_eq_size(InLength, sizeof(QUERY_BUFFER));
119 ok_eq_size(*OutLength, sizeof(QUERY_BUFFER));
121
123
124 ok(ExGetPreviousMode() == UserMode, "Not coming from umode!\n");
125 if (!skip(Buffer && InLength >= sizeof(QUERY_BUFFER) && *OutLength >= sizeof(QUERY_BUFFER), "Cannot read/write from/to buffer!\n"))
126 {
127 PQUERY_BUFFER QueryBuffer;
130
131 QueryBuffer = Buffer;
132 CacheType = (QueryBuffer->Cached ? MmCached : MmNonCached);
133 Length = QueryBuffer->Length;
135 ok(Length > 0, "Null size!\n");
136
137 if (!skip(Length > 0, "Null size!\n"))
138 {
139 if (QueryBuffer->Cached)
140 {
142 ok(CurrentBuffer != NULL, "ExAllocatePool failed!\n");
143 NonCachedLength = 0;
144 }
145 else
146 {
148 ok(CurrentBuffer != NULL, "MmAllocateNonCachedMemory failed!\n");
149 if (CurrentBuffer)
150 {
153 }
154 }
155 if (!skip(CurrentBuffer != NULL, "ExAllocatePool failed!\n"))
156 {
158 ok(CurrentMdl != NULL, "IoAllocateMdl failed!\n");
159 if (!skip(CurrentMdl != NULL, "IoAllocateMdl failed!\n"))
160 {
161 KIRQL Irql;
162
163 SehStatus = STATUS_SUCCESS;
165 {
167 }
169 {
170 SehStatus = _SEH2_GetExceptionCode();
171 }
172 _SEH2_END;
173 ok_eq_hex(SehStatus, STATUS_SUCCESS);
174
176 ok(Irql <= APC_LEVEL, "IRQL > APC_LEVEL: %d\n", Irql);
177
178 SehStatus = STATUS_SUCCESS;
179 _SEH2_TRY
180 {
181 CurrentUser = MmMapLockedPagesSpecifyCache(CurrentMdl, UserMode, CacheType, QueryBuffer->Buffer, FALSE, NormalPagePriority);
182 }
183 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
184 {
185 SehStatus = _SEH2_GetExceptionCode();
186 }
187 _SEH2_END;
188 if (QueryBuffer->Status != -1)
189 {
190 ok_eq_hex(SehStatus, QueryBuffer->Status);
191 if (NT_SUCCESS(QueryBuffer->Status))
192 {
193 ok(CurrentUser != NULL, "MmMapLockedPagesSpecifyCache failed!\n");
194 }
195 else
196 {
197 ok(CurrentUser == NULL, "MmMapLockedPagesSpecifyCache succeeded!\n");
198 }
199 }
200 QueryBuffer->Status = SehStatus;
201 }
202 else
203 {
204 ExFreePoolWithTag(CurrentBuffer, 'MLPC');
205 }
206 }
207 }
208
209 QueryBuffer->Buffer = CurrentUser;
210 *OutLength = sizeof(QUERY_BUFFER);
211 }
212
213 break;
214 }
215 case IOCTL_READ_BUFFER:
216 {
217 ok(Buffer != NULL, "Buffer is NULL\n");
218 ok_eq_size(InLength, sizeof(READ_BUFFER));
219 ok_eq_size(*OutLength, 0);
220 ok(CurrentMdl != NULL, "MDL is not in use!\n");
221
222 if (!skip(Buffer && InLength >= sizeof(READ_BUFFER), "Cannot read from buffer!\n"))
223 {
224 PREAD_BUFFER ReadBuffer;
225
226 ReadBuffer = Buffer;
227 if (!skip(ReadBuffer && ReadBuffer->Buffer == CurrentUser, "Cannot find matching MDL\n"))
228 {
229 if (ReadBuffer->Buffer != NULL)
230 {
231 USHORT i;
232 PULONG KBuffer = MmGetSystemAddressForMdlSafe(CurrentMdl, NormalPagePriority);
233 ok(KBuffer != NULL, "Failed to get kmode ptr\n");
234 ok(ReadBuffer->Length % sizeof(ULONG) == 0, "Invalid size: %d\n", ReadBuffer->Length);
235
236 if (!skip(Buffer != NULL, "Failed to get kmode ptr\n"))
237 {
238 for (i = 0; i < ReadBuffer->Length / sizeof(ULONG); ++i)
239 {
240 ok_eq_ulong(KBuffer[i], ReadBuffer->Pattern);
241 }
242 }
243 }
244 }
245
246 TestCleanEverything();
247 }
248
249 break;
250 }
251 case IOCTL_CLEAN:
252 {
253 TestCleanEverything();
254 break;
255 }
256 default:
257 ok(0, "Got an unknown message! DeviceObject=%p, ControlCode=%lu, Buffer=%p, In=%lu, Out=%lu bytes\n",
258 DeviceObject, ControlCode, Buffer, InLength, *OutLength);
259 break;
260 }
261
262 return Status;
263}
264
265static
266NTSTATUS
267TestIrpHandler(
268 _In_ PDEVICE_OBJECT DeviceObject,
269 _In_ PIRP Irp,
270 _In_ PIO_STACK_LOCATION IoStack)
271{
272 NTSTATUS Status;
273
274 PAGED_CODE();
275
276 DPRINT("IRP %x/%x\n", IoStack->MajorFunction, IoStack->MinorFunction);
277 ASSERT(IoStack->MajorFunction == IRP_MJ_CLEANUP);
278
279 Status = STATUS_NOT_SUPPORTED;
280 Irp->IoStatus.Information = 0;
281
282 if (IoStack->MajorFunction == IRP_MJ_CLEANUP)
283 {
284 TestCleanEverything();
285 Status = STATUS_SUCCESS;
286 }
287
288 if (Status == STATUS_PENDING)
289 {
290 IoMarkIrpPending(Irp);
291 IoCompleteRequest(Irp, IO_NO_INCREMENT);
292 Status = STATUS_PENDING;
293 }
294 else
295 {
296 Irp->IoStatus.Status = Status;
297 IoCompleteRequest(Irp, IO_NO_INCREMENT);
298 }
299
300 return Status;
301}
#define PAGED_CODE()
#define IOCTL_QUERY_BUFFER
static PMDL CurrentMdl
static PVOID CurrentUser
static PVOID CurrentBuffer
static KMT_MESSAGE_HANDLER TestMessageHandler
VOID TestCleanEverything(VOID)
VOID TestUnload(IN PDRIVER_OBJECT DriverObject)
NTSTATUS TestEntry(IN PDRIVER_OBJECT DriverObject, IN PCUNICODE_STRING RegistryPath, OUT PCWSTR *DeviceName, IN OUT INT *Flags)
static KMT_IRP_HANDLER TestIrpHandler
static SIZE_T NonCachedLength
#define read
Definition: acwin.h:96
static unsigned char bytes[4]
Definition: adnsresfilter.c:74
#define ok_eq_pointer(value, expected)
Definition: apitest.h:59
#define ok_eq_hex(value, expected)
Definition: apitest.h:77
#define ok_eq_size(value, expected)
Definition: apitest.h:69
@ Invalid
Definition: asmpp.cpp:30
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
LONG NTSTATUS
Definition: precomp.h:26
void get(int argc, const char *argv[])
Definition: cmds.c:480
Definition: bufpool.h:45
_Out_ PKIRQL Irql
Definition: csq.h:179
static TAGID TAGID find
Definition: db.cpp:155
@ Out
@ In
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
static WCHAR unknown[MAX_STRING_RESOURCE_LEN]
Definition: object.c:1605
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define APC_LEVEL
Definition: env_spec_w32.h:695
#define KeGetCurrentIrql()
Definition: env_spec_w32.h:706
#define NonPagedPool
Definition: env_spec_w32.h:307
#define ExGetPreviousMode
Definition: ex.h:140
#define _SEH2_END
Definition: filesup.c:22
#define _SEH2_TRY
Definition: filesup.c:19
#define IoFreeMdl
Definition: fxmdl.h:89
#define IoAllocateMdl
Definition: fxmdl.h:88
Status
Definition: gdiplustypes.h:25
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLsizeiptr size
Definition: glext.h:5919
GLdouble n
Definition: glext.h:7729
GLuint buffer
Definition: glext.h:5915
GLuint in
Definition: glext.h:9616
GLfloat GLfloat p
Definition: glext.h:8902
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define d
Definition: ke_i.h:81
NTSTATUS KmtRegisterMessageHandler(IN ULONG ControlCode OPTIONAL, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_MESSAGE_HANDLER MessageHandler)
NTSTATUS KmtRegisterIrpHandler(IN UCHAR MajorFunction, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_IRP_HANDLER IrpHandler)
VOID NTAPI MmProbeAndLockPages(IN PMDL Mdl, IN KPROCESSOR_MODE AccessMode, IN LOCK_OPERATION Operation)
Definition: mdlsup.c:931
VOID NTAPI MmUnlockPages(IN PMDL Mdl)
Definition: mdlsup.c:1435
PVOID NTAPI MmMapLockedPagesSpecifyCache(IN PMDL Mdl, IN KPROCESSOR_MODE AccessMode, IN MEMORY_CACHING_TYPE CacheType, IN PVOID BaseAddress, IN ULONG BugCheckOnFailure, IN ULONG Priority)
Definition: mdlsup.c:660
VOID NTAPI MmUnmapLockedPages(IN PVOID BaseAddress, IN PMDL Mdl)
Definition: mdlsup.c:837
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
static PVOID ptr
Definition: dispmode.c:27
VOID NTAPI MmFreeNonCachedMemory(IN PVOID BaseAddress, IN SIZE_T NumberOfBytes)
Definition: ncache.c:172
PVOID NTAPI MmAllocateNonCachedMemory(IN SIZE_T NumberOfBytes)
Definition: ncache.c:25
#define KernelMode
Definition: asm.h:34
#define UserMode
Definition: asm.h:35
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
#define L(x)
Definition: ntvdm.h:50
unsigned short USHORT
Definition: pedump.c:61
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:66
@ Failed
Definition: arc.h:79
#define STATUS_SUCCESS
Definition: shellext.h:65
CardRegion * from
Definition: spigame.cpp:19
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68
Definition: tftpd.h:60
ULONG_PTR * PSIZE_T
Definition: typedefs.h:80
const uint16_t * PCWSTR
Definition: typedefs.h:57
ULONG_PTR SIZE_T
Definition: typedefs.h:80
int32_t INT
Definition: typedefs.h:58
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2055
_Must_inspect_result_ _In_ PWDFDEVICE_INIT _In_opt_ PCUNICODE_STRING DeviceName
Definition: wdfdevice.h:3275
_Must_inspect_result_ _In_ PDRIVER_OBJECT _In_ PCUNICODE_STRING RegistryPath
Definition: wdfdriver.h:215
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
_IRQL_requires_same_ typedef _In_ ULONG ControlCode
Definition: wmitypes.h:55
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
#define IRP_MJ_CLEANUP
@ IoWriteAccess
Definition: ketypes.h:864
_Must_inspect_result_ _In_ PHYSICAL_ADDRESS _In_ PHYSICAL_ADDRESS _In_opt_ PHYSICAL_ADDRESS _In_ MEMORY_CACHING_TYPE CacheType
Definition: mmfuncs.h:217
MDL
Definition: mmtypes.h:117
enum _MEMORY_CACHING_TYPE MEMORY_CACHING_TYPE
@ MmCached
Definition: mmtypes.h:130
@ MmNonCached
Definition: mmtypes.h:129