ReactOS  0.4.14-dev-384-g5b37caa
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 
15 static KMT_IRP_HANDLER TestIrpHandler;
16 static 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 
45 VOID
48 {
49  PAGED_CODE();
50 }
51 
52 VOID
54 {
55  NTSTATUS SehStatus;
56 
57  if (CurrentMdl == NULL)
58  {
59  return;
60  }
61 
62  if (CurrentUser != NULL)
63  {
64  SehStatus = STATUS_SUCCESS;
65  _SEH2_TRY
66  {
68  }
70  {
71  SehStatus = _SEH2_GetExceptionCode();
72  }
73  _SEH2_END;
74  ok_eq_hex(SehStatus, STATUS_SUCCESS);
75  CurrentUser = NULL;
76  }
77 
78  SehStatus = STATUS_SUCCESS;
79  _SEH2_TRY
80  {
82  }
84  {
85  SehStatus = _SEH2_GetExceptionCode();
86  }
87  _SEH2_END;
88  ok_eq_hex(SehStatus, STATUS_SUCCESS);
90  if (NonCachedLength)
91  {
93  }
94  else
95  {
97  }
98  CurrentMdl = NULL;
99 }
100 
101 static
102 NTSTATUS
107  IN SIZE_T InLength,
108  IN OUT PSIZE_T OutLength)
109 {
111  NTSTATUS SehStatus;
112 
113  switch (ControlCode)
114  {
115  case IOCTL_QUERY_BUFFER:
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;
128  USHORT Length;
130 
131  QueryBuffer = Buffer;
132  CacheType = (QueryBuffer->Cached ? MmCached : MmNonCached);
133  Length = QueryBuffer->Length;
134  CurrentUser = NULL;
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;
164  _SEH2_TRY
165  {
167  }
169  {
170  SehStatus = _SEH2_GetExceptionCode();
171  }
172  _SEH2_END;
173  ok_eq_hex(SehStatus, STATUS_SUCCESS);
174 
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 
265 static
266 NTSTATUS
267 TestIrpHandler(
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 KeGetCurrentIrql()
Definition: env_spec_w32.h:706
const uint16_t * PCWSTR
Definition: typedefs.h:55
#define IN
Definition: typedefs.h:38
Definition: tftpd.h:59
#define ok_eq_size(value, expected)
Definition: kmt_test.h:247
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:323
#define ok_eq_pointer(value, expected)
static PMDL CurrentMdl
LONG NTSTATUS
Definition: precomp.h:26
GLdouble n
Definition: glext.h:7729
GLuint buffer
Definition: glext.h:5915
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
VOID NTAPI MmUnlockPages(IN PMDL Mdl)
Definition: mdlsup.c:1439
KPROCESSOR_MODE NTAPI ExGetPreviousMode(VOID)
Definition: sysinfo.c:3066
IRP
Definition: iotypes.h:2463
WCHAR DeviceName[]
Definition: adapter.cpp:21
_Out_ PKIRQL Irql
Definition: csq.h:179
int32_t INT
Definition: typedefs.h:56
#define PAGED_CODE()
Definition: video.h:57
_SEH2_TRY
Definition: create.c:4250
UCHAR KIRQL
Definition: env_spec_w32.h:591
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
ULONG_PTR * PSIZE_T
Definition: typedefs.h:78
static unsigned char bytes[4]
Definition: adnsresfilter.c:74
VOID NTAPI MmUnmapLockedPages(IN PVOID BaseAddress, IN PMDL Mdl)
Definition: mdlsup.c:841
VOID TestCleanEverything(VOID)
#define IOCTL_QUERY_BUFFER
static PVOID ptr
Definition: dispmode.c:27
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
smooth NULL
Definition: ftsmooth.c:416
_Must_inspect_result_ _In_ PHYSICAL_ADDRESS _In_ PHYSICAL_ADDRESS _In_opt_ PHYSICAL_ADDRESS _In_ MEMORY_CACHING_TYPE CacheType
Definition: mmfuncs.h:214
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
_IRQL_requires_same_ typedef _In_ ULONG ControlCode
Definition: wmitypes.h:55
Definition: bufpool.h:45
NTSTATUS KmtRegisterIrpHandler(IN UCHAR MajorFunction, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_IRP_HANDLER IrpHandler)
Definition: arc.h:79
void get(int argc, const char *argv[])
Definition: cmds.c:480
GLsizeiptr size
Definition: glext.h:5919
NTSTATUS KmtRegisterMessageHandler(IN ULONG ControlCode OPTIONAL, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_MESSAGE_HANDLER MessageHandler)
#define d
Definition: ke_i.h:81
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
Definition: id3.c:18
VOID NTAPI IoFreeMdl(PMDL Mdl)
Definition: iomdl.c:146
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
MDL
Definition: mmtypes.h:117
static const WCHAR L[]
Definition: oid.c:1250
NTSTATUS TestEntry(IN PDRIVER_OBJECT DriverObject, IN PCUNICODE_STRING RegistryPath, OUT PCWSTR *DeviceName, IN OUT INT *Flags)
Status
Definition: gdiplustypes.h:24
PMDL NTAPI IoAllocateMdl(IN PVOID VirtualAddress, IN ULONG Length, IN BOOLEAN SecondaryBuffer, IN BOOLEAN ChargeQuota, IN PIRP Irp)
Definition: iomdl.c:22
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
ULONG_PTR SIZE_T
Definition: typedefs.h:78
_SEH2_END
Definition: create.c:4424
VOID NTAPI MmProbeAndLockPages(IN PMDL Mdl, IN KPROCESSOR_MODE AccessMode, IN LOCK_OPERATION Operation)
Definition: mdlsup.c:935
GLuint in
Definition: glext.h:9616
PVOID NTAPI MmMapLockedPagesSpecifyCache(IN PMDL Mdl, IN KPROCESSOR_MODE AccessMode, IN MEMORY_CACHING_TYPE CacheType, IN PVOID BaseAddress, IN ULONG BugCheckOnFailure, IN MM_PAGE_PRIORITY Priority)
Definition: mdlsup.c:664
unsigned short USHORT
Definition: pedump.c:61
static KMT_IRP_HANDLER TestIrpHandler
#define ok(value,...)
Definition: atltest.h:57
static PVOID CurrentBuffer
VOID TestUnload(IN PDRIVER_OBJECT DriverObject)
VOID NTAPI MmFreeNonCachedMemory(IN PVOID BaseAddress, IN SIZE_T NumberOfBytes)
Definition: ncache.c:172
#define skip(...)
Definition: atltest.h:64
static TAGID TAGID find
Definition: db.cpp:153
#define IRP_MJ_CLEANUP
#define OUT
Definition: typedefs.h:39
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
_In_ PUNICODE_STRING RegistryPath
Definition: wmip.h:27
static PVOID CurrentUser
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define ok_eq_hex(value, expected)
static KMT_MESSAGE_HANDLER TestMessageHandler
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
GLfloat GLfloat p
Definition: glext.h:8902
CardRegion * from
Definition: spigame.cpp:19
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
enum _MEMORY_CACHING_TYPE MEMORY_CACHING_TYPE
return STATUS_SUCCESS
Definition: btrfs.c:2938
PVOID NTAPI MmAllocateNonCachedMemory(IN SIZE_T NumberOfBytes)
Definition: ncache.c:25
#define APC_LEVEL
Definition: env_spec_w32.h:695
static SIZE_T NonCachedLength
_CRTIMP int __cdecl read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68