aa0a84bd21a6d453b59e56f12893971f95d0bcf7
[reactos.git] / rostests / kmtests / include / kmt_test_kernel.h
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 test framework declarations
5 * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6 */
7
8 #ifndef _KMTEST_TEST_KERNEL_H_
9 #define _KMTEST_TEST_KERNEL_H_
10
11 #if !defined _KMTEST_TEST_H_
12 #error include kmt_test.h instead of including kmt_test_kernel.h
13 #endif /* !defined _KMTEST_TEST_H_ */
14
15 BOOLEAN KmtIsCheckedBuild;
16 BOOLEAN KmtIsMultiProcessorBuild;
17 PCSTR KmtMajorFunctionNames[] =
18 {
19 "Create",
20 "CreateNamedPipe",
21 "Close",
22 "Read",
23 "Write",
24 "QueryInformation",
25 "SetInformation",
26 "QueryEa",
27 "SetEa",
28 "FlushBuffers",
29 "QueryVolumeInformation",
30 "SetVolumeInformation",
31 "DirectoryControl",
32 "FileSystemControl",
33 "DeviceControl",
34 "InternalDeviceControl/Scsi",
35 "Shutdown",
36 "LockControl",
37 "Cleanup",
38 "CreateMailslot",
39 "QuerySecurity",
40 "SetSecurity",
41 "Power",
42 "SystemControl",
43 "DeviceChange",
44 "QueryQuota",
45 "SetQuota",
46 "Pnp/PnpPower"
47 };
48
49 VOID KmtSetIrql(IN KIRQL NewIrql)
50 {
51 KIRQL Irql = KeGetCurrentIrql();
52 if (Irql > NewIrql)
53 KeLowerIrql(NewIrql);
54 else if (Irql < NewIrql)
55 KeRaiseIrql(NewIrql, &Irql);
56 }
57
58 BOOLEAN KmtAreInterruptsEnabled(VOID)
59 {
60 return (__readeflags() & (1 << 9)) != 0;
61 }
62
63 typedef struct _POOL_HEADER
64 {
65 union
66 {
67 struct
68 {
69 #ifdef _M_AMD64
70 USHORT PreviousSize:8;
71 USHORT PoolIndex:8;
72 USHORT BlockSize:8;
73 USHORT PoolType:8;
74 #else
75 USHORT PreviousSize:9;
76 USHORT PoolIndex:7;
77 USHORT BlockSize:9;
78 USHORT PoolType:7;
79 #endif
80 };
81 ULONG Ulong1;
82 };
83 #ifdef _M_AMD64
84 ULONG PoolTag;
85 #endif
86 union
87 {
88 #ifdef _M_AMD64
89 PEPROCESS ProcessBilled;
90 #else
91 ULONG PoolTag;
92 #endif
93 struct
94 {
95 USHORT AllocatorBackTraceIndex;
96 USHORT PoolTagHash;
97 };
98 };
99 } POOL_HEADER, *PPOOL_HEADER;
100
101 ULONG KmtGetPoolTag(PVOID Memory)
102 {
103 PPOOL_HEADER Header;
104
105 /* it's not so easy for allocations of PAGE_SIZE */
106 if (((ULONG_PTR)Memory & (PAGE_SIZE - 1)) == 0)
107 return 'TooL';
108
109 Header = Memory;
110 Header--;
111
112 return Header->PoolTag;
113 }
114
115 USHORT KmtGetPoolType(PVOID Memory)
116 {
117 PPOOL_HEADER Header;
118
119 /* it's not so easy for allocations of PAGE_SIZE */
120 if (((ULONG_PTR)Memory & (PAGE_SIZE - 1)) == 0)
121 return 0;
122
123 Header = Memory;
124 Header--;
125
126 return Header->PoolType;
127 }
128
129 PKTHREAD KmtStartThread(IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext OPTIONAL)
130 {
131 NTSTATUS Status;
132 OBJECT_ATTRIBUTES ObjectAttributes;
133 HANDLE ThreadHandle;
134 PVOID ThreadObject = NULL;
135
136 InitializeObjectAttributes(&ObjectAttributes,
137 NULL,
138 OBJ_KERNEL_HANDLE,
139 NULL,
140 NULL);
141 ThreadHandle = INVALID_HANDLE_VALUE;
142 Status = PsCreateSystemThread(&ThreadHandle,
143 SYNCHRONIZE,
144 &ObjectAttributes,
145 NULL,
146 NULL,
147 StartRoutine,
148 StartContext);
149 ok_eq_hex(Status, STATUS_SUCCESS);
150 if (!skip(NT_SUCCESS(Status) && ThreadHandle != NULL && ThreadHandle != INVALID_HANDLE_VALUE, "No thread\n"))
151 {
152 Status = ObReferenceObjectByHandle(ThreadHandle,
153 SYNCHRONIZE,
154 *PsThreadType,
155 KernelMode,
156 &ThreadObject,
157 NULL);
158 ok_eq_hex(Status, STATUS_SUCCESS);
159 ObCloseHandle(ThreadHandle, KernelMode);
160 }
161 return ThreadObject;
162 }
163
164 VOID KmtFinishThread(IN PKTHREAD Thread OPTIONAL, IN PKEVENT Event OPTIONAL)
165 {
166 NTSTATUS Status;
167
168 if (skip(Thread != NULL, "No thread\n"))
169 return;
170
171 if (Event)
172 KeSetEvent(Event, IO_NO_INCREMENT, TRUE);
173 Status = KeWaitForSingleObject(Thread,
174 Executive,
175 KernelMode,
176 FALSE,
177 NULL);
178 ok_eq_hex(Status, STATUS_SUCCESS);
179 ObDereferenceObject(Thread);
180 }
181
182 INT __cdecl KmtVSNPrintF(PSTR Buffer, SIZE_T BufferMaxLength, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 3, 0);
183
184 #endif /* !defined _KMTEST_TEST_KERNEL_H_ */