2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Kernel-Mode Test Suite Interlocked function test
5 * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
10 /* missing prototypes >:| */
11 __declspec(dllimport
) long __fastcall
InterlockedCompareExchange(volatile long *, long, long);
12 __declspec(dllimport
) __int64 __fastcall
ExInterlockedCompareExchange64(volatile __int64
*, __int64
*, __int64
*, void *);
13 __declspec(dllimport
) __int64 __fastcall
ExfInterlockedCompareExchange64(volatile __int64
*, __int64
*, __int64
*);
14 __declspec(dllimport
) long __fastcall
InterlockedExchange(volatile long *, long);
15 __declspec(dllimport
) unsigned long __stdcall
ExInterlockedExchangeUlong(unsigned long *, unsigned long, void *);
16 __declspec(dllimport
) long __fastcall
InterlockedExchangeAdd(volatile long *, long);
18 __declspec(dllimport
) unsigned long __stdcall
ExInterlockedAddUlong(unsigned long *, unsigned long, unsigned long *);
20 __declspec(dllimport
) unsigned long __stdcall
Exi386InterlockedExchangeUlong(unsigned long *, unsigned long);
21 __declspec(dllimport
) long __fastcall
InterlockedIncrement(long *);
22 __declspec(dllimport
) long __fastcall
InterlockedDecrement(long *);
23 __declspec(dllimport
) int __stdcall
ExInterlockedIncrementLong(long *, void *);
24 __declspec(dllimport
) int __stdcall
ExInterlockedDecrementLong(long *, void *);
25 __declspec(dllimport
) int __stdcall
Exi386InterlockedIncrementLong(long *);
26 __declspec(dllimport
) int __stdcall
Exi386InterlockedDecrementLong(long *);
30 /* TODO: There are quite some changes needed for other architectures!
31 ExInterlockedAddLargeInteger, ExInterlockedAddUlong are the only two
32 functions actually exported by my win7/x64 kernel! */
34 /* TODO: stress-testing */
36 static KSPIN_LOCK SpinLock
;
41 unsigned long esi
, edi
, ebx
, ebp
, esp
;
43 #elif defined(_M_AMD64)
46 unsigned long long rsi
, rdi
, rbx
, rbp
, rsp
, r12
, r13
, r14
, r15
;
50 typedef int PROCESSOR_STATE
;
53 /* TODO: these need to be rewritten in proper assembly to account for registers
54 * saved by the caller */
55 #if defined(_MSC_VER) && defined(_M_IX86)
56 #define SaveState(State) do \
58 __asm lea ecx, [State] \
59 __asm mov [ecx], esi \
60 __asm mov [ecx+4], edi \
61 __asm mov [ecx+8], ebx \
62 __asm mov [ecx+12], ebp \
63 __asm mov [ecx+16], esp \
66 #define CheckState(OldState, NewState) do \
68 /* TODO: MSVC uses esi and saves it before, so this is okay */ \
69 /*ok_eq_hex((OldState)->esi, (NewState)->esi);*/ \
70 ok_eq_hex((OldState)->edi, (NewState)->edi); \
71 ok_eq_hex((OldState)->ebx, (NewState)->ebx); \
72 ok_eq_hex((OldState)->ebp, (NewState)->ebp); \
73 ok_eq_hex((OldState)->esp, (NewState)->esp); \
76 #elif defined(__GNUC__) && defined(_M_IX86)
77 #define SaveState(State) \
79 "movl\t%%esi, (%%ecx)\n\t" \
80 "movl\t%%edi, 4(%%ecx)\n\t" \
81 "movl\t%%ebx, 8(%%ecx)\n\t" \
82 "movl\t%%ebp, 12(%%ecx)\n\t" \
83 "movl\t%%esp, 16(%%ecx)" \
84 : : "c" (&State) : "memory" \
87 #define CheckState(OldState, NewState) do \
89 /* TODO: GCC 4.7 uses esi and saves it before, so this is okay */ \
90 /*ok_eq_hex((OldState)->esi, (NewState)->esi);*/ \
91 ok_eq_hex((OldState)->edi, (NewState)->edi); \
92 /* TODO: GCC 4.4 uses ebx and saves it before, so this is okay */ \
93 /*ok_eq_hex((OldState)->ebx, (NewState)->ebx);*/ \
94 ok_eq_hex((OldState)->ebp, (NewState)->ebp); \
95 ok_eq_hex((OldState)->esp, (NewState)->esp); \
97 #elif defined(__GNUC__) && defined(_M_AMD64)
98 #define SaveState(State) \
100 "mov\t%%rsi, (%%rcx)\n\t" \
101 "mov\t%%rdi, 8(%%rcx)\n\t" \
102 "mov\t%%rbx, 16(%%rcx)\n\t" \
103 "mov\t%%rbp, 24(%%rcx)\n\t" \
104 "mov\t%%rsp, 32(%%rcx)\n\t" \
105 "mov\t%%r12, 40(%%rcx)\n\t" \
106 "mov\t%%r13, 48(%%rcx)\n\t" \
107 "mov\t%%r14, 56(%%rcx)\n\t" \
108 "mov\t%%r15, 64(%%rcx)" \
109 : : "c" (&State) : "memory" \
112 #define CheckState(OldState, NewState) do \
114 ok_eq_hex((OldState)->rsi, (NewState)->rsi); \
115 ok_eq_hex((OldState)->rdi, (NewState)->rdi); \
116 ok_eq_hex((OldState)->rbx, (NewState)->rbx); \
117 ok_eq_hex((OldState)->rbp, (NewState)->rbp); \
118 ok_eq_hex((OldState)->rsp, (NewState)->rsp); \
119 ok_eq_hex((OldState)->r12, (NewState)->r12); \
120 ok_eq_hex((OldState)->r13, (NewState)->r13); \
121 ok_eq_hex((OldState)->r14, (NewState)->r14); \
122 ok_eq_hex((OldState)->r15, (NewState)->r15); \
125 #define SaveState(State)
126 #define CheckState(OldState, NewState) do \
139 Ret
.QuadPart
= Value
;
143 #define CheckInterlockedCmpXchg(Function, Type, Print, Val, Cmp, Xchg, \
144 ExpectedValue, ExpectedRet) do \
146 Type Ret##Type = 0; \
147 Type Value##Type = Val; \
148 Status = STATUS_SUCCESS; \
150 SaveState(OldState); \
151 Ret##Type = Function(&Value##Type, Xchg, Cmp); \
152 SaveState(NewState); \
153 CheckState(&OldState, &NewState); \
154 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { \
155 Status = _SEH2_GetExceptionCode(); \
157 ok_eq_hex(Status, STATUS_SUCCESS); \
158 ok_eq_print(Ret##Type, ExpectedRet, Print); \
159 ok_eq_print(Value##Type, ExpectedValue, Print); \
162 #define CheckInterlockedCmpXchgI(Function, Type, Print, Val, Cmp, Xchg, \
163 ExpectedValue, ExpectedRet, ...) do \
165 Type Ret##Type = 0; \
166 Type Value##Type = Val; \
167 Type Compare##Type = Cmp; \
168 Type Exchange##Type = Xchg; \
169 Status = STATUS_SUCCESS; \
171 SaveState(OldState); \
172 Ret##Type = Function(&Value##Type, &Exchange##Type, \
173 &Compare##Type, ##__VA_ARGS__); \
174 SaveState(NewState); \
175 CheckState(&OldState, &NewState); \
176 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { \
177 Status = _SEH2_GetExceptionCode(); \
179 ok_eq_hex(Status, STATUS_SUCCESS); \
180 ok_eq_print(Ret##Type, ExpectedRet, Print); \
181 ok_eq_print(Value##Type, ExpectedValue, Print); \
182 ok_eq_print(Exchange##Type, Xchg, Print); \
183 ok_eq_print(Compare##Type, Cmp, Print); \
186 #define CheckInterlockedOp(Function, Type, Print, Val, Op, \
187 ExpectedValue, ExpectedRet, ...) do \
189 Type Ret##Type = 0; \
190 Type Value##Type = Val; \
191 Status = STATUS_SUCCESS; \
193 SaveState(OldState); \
194 Ret##Type = Function(&Value##Type, Op, ##__VA_ARGS__); \
195 SaveState(NewState); \
196 CheckState(&OldState, &NewState); \
197 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { \
198 Status = _SEH2_GetExceptionCode(); \
200 ok_eq_hex(Status, STATUS_SUCCESS); \
201 ok_eq_print(Ret##Type, ExpectedRet, Print); \
202 ok_eq_print(Value##Type, ExpectedValue, Print); \
205 #define CheckInterlockedOpNoArg(Function, Type, Print, Val, \
206 ExpectedValue, ExpectedRet, ...) do \
208 Type Ret##Type = 0; \
209 Type Value##Type = Val; \
210 Status = STATUS_SUCCESS; \
212 SaveState(OldState); \
213 Ret##Type = Function(&Value##Type, ##__VA_ARGS__); \
214 SaveState(NewState); \
215 CheckState(&OldState, &NewState); \
216 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { \
217 Status = _SEH2_GetExceptionCode(); \
219 ok_eq_hex(Status, STATUS_SUCCESS); \
220 ok_eq_print(Ret##Type, ExpectedRet, Print); \
221 ok_eq_print(Value##Type, ExpectedValue, Print); \
224 #define CheckInterlockedOpLarge(Function, Type, Print, Val, Op, \
225 ExpectedValue, ExpectedRet, ...) do \
227 Type Ret##Type = Large(0); \
228 Type Value##Type = Val; \
229 Status = STATUS_SUCCESS; \
231 SaveState(OldState); \
232 Ret##Type = Function(&Value##Type, Op, ##__VA_ARGS__); \
233 SaveState(NewState); \
234 CheckState(&OldState, &NewState); \
235 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { \
236 Status = _SEH2_GetExceptionCode(); \
238 ok_eq_hex(Status, STATUS_SUCCESS); \
239 ok_eq_print(Ret##Type.QuadPart, ExpectedRet, Print); \
240 ok_eq_print(Value##Type.QuadPart, ExpectedValue, Print); \
243 #define CheckInterlockedOpLargeNoRet(Function, Type, Print, Val, Op, \
246 Type Value##Type = Val; \
247 Status = STATUS_SUCCESS; \
249 SaveState(OldState); \
250 Function(&Value##Type, Op); \
251 SaveState(NewState); \
252 CheckState(&OldState, &NewState); \
253 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { \
254 Status = _SEH2_GetExceptionCode(); \
256 ok_eq_hex(Status, STATUS_SUCCESS); \
257 ok_eq_print(Value##Type.QuadPart, ExpectedValue, Print); \
260 /* TODO: missing in wdm.h! */
261 #define InterlockedCompareExchangeAcquire InterlockedCompareExchange
262 #define InterlockedCompareExchangeRelease InterlockedCompareExchange
263 #define InterlockedIncrementAcquire InterlockedIncrement
264 #define InterlockedIncrementRelease InterlockedIncrement
265 #define InterlockedDecrementAcquire InterlockedDecrement
266 #define InterlockedDecrementRelease InterlockedDecrement
270 TestInterlockedFunctional(VOID
)
273 PKSPIN_LOCK pSpinLock
= &SpinLock
;
274 PROCESSOR_STATE OldState
, NewState
;
276 /* on x86, most of these are supported intrinsically and don't need a spinlock! */
277 #if defined _M_IX86 || defined _M_AMD64
281 /* CompareExchange */
283 CheckInterlockedCmpXchg(InterlockedCompareExchange
, LONG
, "%ld", 5, 6, 8, 5L, 5L);
284 CheckInterlockedCmpXchg(InterlockedCompareExchange
, LONG
, "%ld", 5, 5, 9, 9L, 5L);
285 /* these only exist as macros on x86 */
286 CheckInterlockedCmpXchg(InterlockedCompareExchangeAcquire
, LONG
, "%ld", 16, 9, 12, 16L, 16L);
287 CheckInterlockedCmpXchg(InterlockedCompareExchangeAcquire
, LONG
, "%ld", 16, 16, 4, 4L, 16L);
288 CheckInterlockedCmpXchg(InterlockedCompareExchangeRelease
, LONG
, "%ld", 27, 123, 38, 27L, 27L);
289 CheckInterlockedCmpXchg(InterlockedCompareExchangeRelease
, LONG
, "%ld", 27, 27, 39, 39L, 27L);
290 /* exported function */
291 #undef InterlockedCompareExchange
293 CheckInterlockedCmpXchg(InterlockedCompareExchange
, LONG
, "%ld", 5, 6, 8, 5L, 5L);
294 CheckInterlockedCmpXchg(InterlockedCompareExchange
, LONG
, "%ld", 5, 5, 9, 9L, 5L);
296 /* only exists as a macro */
297 CheckInterlockedCmpXchg(InterlockedCompareExchangePointer
, PVOID
, "%p", (PVOID
)117, (PVOID
)711, (PVOID
)12, (PVOID
)117, (PVOID
)117);
298 CheckInterlockedCmpXchg(InterlockedCompareExchangePointer
, PVOID
, "%p", (PVOID
)117, (PVOID
)117, (PVOID
)228, (PVOID
)228, (PVOID
)117);
300 CheckInterlockedCmpXchgI(ExInterlockedCompareExchange64
, LONGLONG
, "%I64d", 17, 4LL, 20LL, 17LL, 17LL, pSpinLock
);
301 CheckInterlockedCmpXchgI(ExInterlockedCompareExchange64
, LONGLONG
, "%I64d", 17, 17LL, 21LL, 21LL, 17LL, pSpinLock
);
303 /* exported function */
304 CheckInterlockedCmpXchgI((ExInterlockedCompareExchange64
), LONGLONG
, "%I64d", 17, 4LL, 20LL, 17LL, 17LL, pSpinLock
);
305 CheckInterlockedCmpXchgI((ExInterlockedCompareExchange64
), LONGLONG
, "%I64d", 17, 17LL, 21LL, 21LL, 17LL, pSpinLock
);
306 /* fastcall version */
307 CheckInterlockedCmpXchgI(ExfInterlockedCompareExchange64
, LONGLONG
, "%I64d", 17, 4LL, 20LL, 17LL, 17LL);
308 CheckInterlockedCmpXchgI(ExfInterlockedCompareExchange64
, LONGLONG
, "%I64d", 17, 17LL, 21LL, 21LL, 17LL);
312 CheckInterlockedOp(InterlockedExchange
, LONG
, "%ld", 5, 8, 8L, 5L);
313 CheckInterlockedOpNoArg(InterlockedExchangePointer
, PVOID
, "%p", (PVOID
)700, (PVOID
)93, (PVOID
)700, (PVOID
)93);
314 #undef InterlockedExchange
316 CheckInterlockedOp(InterlockedExchange
, LONG
, "%ld", 5, 8, 8L, 5L);
317 CheckInterlockedOp(ExInterlockedExchangeUlong
, ULONG
, "%lu", 212, 121, 121LU, 212LU, pSpinLock
);
318 CheckInterlockedOp((ExInterlockedExchangeUlong
), ULONG
, "%lu", 212, 121, 121LU, 212LU, pSpinLock
);
319 CheckInterlockedOp(Exi386InterlockedExchangeUlong
, ULONG
, "%lu", 212, 121, 121LU, 212LU);
320 CheckInterlockedOp(Exfi386InterlockedExchangeUlong
, ULONG
, "%lu", 212, 121, 121LU, 212LU);
324 /* TODO: ExInterlockedExchangeAddLargeInteger? */
325 CheckInterlockedOp(InterlockedExchangeAdd
, LONG
, "%ld", 312, 7, 319L, 312L);
326 #undef InterlockedExchangeAdd
328 CheckInterlockedOp(InterlockedExchangeAdd
, LONG
, "%ld", 312, 7, 319L, 312L);
332 /* these DO need a valid spinlock even on x86 */
333 CheckInterlockedOpLarge(ExInterlockedAddLargeInteger
, LARGE_INTEGER
, "%I64d", Large(23), Large(7), 30LL, 23LL, &SpinLock
);
334 CheckInterlockedOpLargeNoRet(ExInterlockedAddLargeStatistic
, LARGE_INTEGER
, "%I64d", Large(15), 17LL, 32LL);
335 CheckInterlockedOp(ExInterlockedAddUlong
, ULONG
, "%lu", 239, 44, 283LU, 239LU, &SpinLock
);
336 #undef ExInterlockedAddUlong
337 CheckInterlockedOp(ExInterlockedAddUlong
, ULONG
, "%lu", 239, 44, 283LU, 239LU, &SpinLock
);
340 CheckInterlockedOpNoArg(InterlockedIncrement
, LONG
, "%ld", 2341L, 2342L, 2342L);
341 CheckInterlockedOpNoArg(InterlockedIncrement
, LONG
, "%ld", (LONG
)MAXLONG
, (LONG
)MINLONG
, (LONG
)MINLONG
);
342 CheckInterlockedOpNoArg(InterlockedIncrementAcquire
, LONG
, "%ld", 2341L, 2342L, 2342L);
343 CheckInterlockedOpNoArg(InterlockedIncrementRelease
, LONG
, "%ld", 2341L, 2342L, 2342L);
344 #undef InterlockedIncrement
346 CheckInterlockedOpNoArg(InterlockedIncrement
, LONG
, "%ld", 2341L, 2342L, 2342L);
347 CheckInterlockedOpNoArg(InterlockedIncrement
, LONG
, "%ld", (LONG
)MAXLONG
, (LONG
)MINLONG
, (LONG
)MINLONG
);
348 CheckInterlockedOpNoArg(ExInterlockedIncrementLong
, LONG
, "%ld", -2L, -1L, (LONG
)ResultNegative
, pSpinLock
);
349 CheckInterlockedOpNoArg(ExInterlockedIncrementLong
, LONG
, "%ld", -1L, 0L, (LONG
)ResultZero
, pSpinLock
);
350 CheckInterlockedOpNoArg(ExInterlockedIncrementLong
, LONG
, "%ld", 0L, 1L, (LONG
)ResultPositive
, pSpinLock
);
351 CheckInterlockedOpNoArg(ExInterlockedIncrementLong
, LONG
, "%ld", (LONG
)MAXLONG
, (LONG
)MINLONG
, (LONG
)ResultNegative
, pSpinLock
);
352 CheckInterlockedOpNoArg((ExInterlockedIncrementLong
), LONG
, "%ld", -2L, -1L, (LONG
)ResultNegative
, pSpinLock
);
353 CheckInterlockedOpNoArg((ExInterlockedIncrementLong
), LONG
, "%ld", -1L, 0L, (LONG
)ResultZero
, pSpinLock
);
354 CheckInterlockedOpNoArg((ExInterlockedIncrementLong
), LONG
, "%ld", 0L, 1L, (LONG
)ResultPositive
, pSpinLock
);
355 CheckInterlockedOpNoArg((ExInterlockedIncrementLong
), LONG
, "%ld", (LONG
)MAXLONG
, (LONG
)MINLONG
, (LONG
)ResultNegative
, pSpinLock
);
356 CheckInterlockedOpNoArg(Exi386InterlockedIncrementLong
, LONG
, "%ld", -2L, -1L, (LONG
)ResultNegative
);
357 CheckInterlockedOpNoArg(Exi386InterlockedIncrementLong
, LONG
, "%ld", -1L, 0L, (LONG
)ResultZero
);
358 CheckInterlockedOpNoArg(Exi386InterlockedIncrementLong
, LONG
, "%ld", 0L, 1L, (LONG
)ResultPositive
);
359 CheckInterlockedOpNoArg(Exi386InterlockedIncrementLong
, LONG
, "%ld", (LONG
)MAXLONG
, (LONG
)MINLONG
, (LONG
)ResultNegative
);
363 CheckInterlockedOpNoArg(InterlockedDecrement
, LONG
, "%ld", 1745L, 1744L, 1744L);
364 CheckInterlockedOpNoArg(InterlockedDecrement
, LONG
, "%ld", (LONG
)MINLONG
, (LONG
)MAXLONG
, (LONG
)MAXLONG
);
365 CheckInterlockedOpNoArg(InterlockedDecrementAcquire
, LONG
, "%ld", 1745L, 1744L, 1744L);
366 CheckInterlockedOpNoArg(InterlockedDecrementRelease
, LONG
, "%ld", 1745L, 1744L, 1744L);
367 #undef InterlockedDecrement
369 CheckInterlockedOpNoArg(InterlockedDecrement
, LONG
, "%ld", 1745L, 1744L, 1744L);
370 CheckInterlockedOpNoArg(InterlockedDecrement
, LONG
, "%ld", (LONG
)MINLONG
, (LONG
)MAXLONG
, (LONG
)MAXLONG
);
371 CheckInterlockedOpNoArg(ExInterlockedDecrementLong
, LONG
, "%ld", (LONG
)MINLONG
, (LONG
)MAXLONG
, (LONG
)ResultPositive
, pSpinLock
);
372 CheckInterlockedOpNoArg(ExInterlockedDecrementLong
, LONG
, "%ld", 0L, -1L, (LONG
)ResultNegative
, pSpinLock
);
373 CheckInterlockedOpNoArg(ExInterlockedDecrementLong
, LONG
, "%ld", 1L, 0L, (LONG
)ResultZero
, pSpinLock
);
374 CheckInterlockedOpNoArg(ExInterlockedDecrementLong
, LONG
, "%ld", 2L, 1L, (LONG
)ResultPositive
, pSpinLock
);
375 CheckInterlockedOpNoArg((ExInterlockedDecrementLong
), LONG
, "%ld", (LONG
)MINLONG
, (LONG
)MAXLONG
, (LONG
)ResultPositive
, pSpinLock
);
376 CheckInterlockedOpNoArg((ExInterlockedDecrementLong
), LONG
, "%ld", 0L, -1L, (LONG
)ResultNegative
, pSpinLock
);
377 CheckInterlockedOpNoArg((ExInterlockedDecrementLong
), LONG
, "%ld", 1L, 0L, (LONG
)ResultZero
, pSpinLock
);
378 CheckInterlockedOpNoArg((ExInterlockedDecrementLong
), LONG
, "%ld", 2L, 1L, (LONG
)ResultPositive
, pSpinLock
);
379 CheckInterlockedOpNoArg(Exi386InterlockedDecrementLong
, LONG
, "%ld", (LONG
)MINLONG
, (LONG
)MAXLONG
, (LONG
)ResultPositive
);
380 CheckInterlockedOpNoArg(Exi386InterlockedDecrementLong
, LONG
, "%ld", 0L, -1L, (LONG
)ResultNegative
);
381 CheckInterlockedOpNoArg(Exi386InterlockedDecrementLong
, LONG
, "%ld", 1L, 0L, (LONG
)ResultZero
);
382 CheckInterlockedOpNoArg(Exi386InterlockedDecrementLong
, LONG
, "%ld", 2L, 1L, (LONG
)ResultPositive
);
386 CheckInterlockedOp(InterlockedAnd
, LONG
, "0x%lx", 0x1234L
, 0x1111L
, 0x1010L
, 0x1234L
);
387 CheckInterlockedOp(InterlockedOr
, LONG
, "0x%lx", 0x1234L
, 0x1111L
, 0x1335L
, 0x1234L
);
388 CheckInterlockedOp(InterlockedXor
, LONG
, "0x%lx", 0x1234L
, 0x1111L
, 0x0325L
, 0x1234L
);
390 CheckInterlockedOp(InterlockedXor64
, LONGLONG
, "0x%I64x", 0x200001234LL
, 0x100001111LL
, 0x300000325LL
, 0x200001234LL
);
394 START_TEST(ExInterlocked
)
397 KeInitializeSpinLock(&SpinLock
);
399 /* functional testing */
400 TestInterlockedFunctional();
401 KeRaiseIrql(HIGH_LEVEL
, &Irql
);
402 TestInterlockedFunctional();