1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * PROJECT:         ReactOS kernel-mode tests
 * LICENSE:         GPLv2+ - See COPYING in the top level directory
 * PURPOSE:         Kernel-Mode Test Suite Interlocked function test
 * PROGRAMMER:      Thomas Faber <thomas.faber@reactos.org>
 */

#include <stddef.h>

/* missing prototypes >:| */
__declspec(dllimport)   long            __fastcall  InterlockedCompareExchange(volatile long *, long, long);
__declspec(dllimport)   __int64         __fastcall  ExInterlockedCompareExchange64(volatile __int64 *, __int64 *, __int64 *, void *);
__declspec(dllimport)   __int64         __fastcall  ExfInterlockedCompareExchange64(volatile __int64 *, __int64 *, __int64 *);
__declspec(dllimport)   long            __fastcall  InterlockedExchange(volatile long *, long);
__declspec(dllimport)   unsigned long   __stdcall   ExInterlockedExchangeUlong(unsigned long *, unsigned long, void *);
__declspec(dllimport)   long            __fastcall  InterlockedExchangeAdd(volatile long *, long);
#ifdef _X86_
__declspec(dllimport)   unsigned long   __stdcall   ExInterlockedAddUlong(unsigned long *, unsigned long, unsigned long *);
#endif
__declspec(dllimport)   unsigned long   __stdcall   Exi386InterlockedExchangeUlong(unsigned long *, unsigned long);
__declspec(dllimport)   long            __fastcall  InterlockedIncrement(long *);
__declspec(dllimport)   long            __fastcall  InterlockedDecrement(long *);
__declspec(dllimport)   int             __stdcall   ExInterlockedIncrementLong(long *, void *);
__declspec(dllimport)   int             __stdcall   ExInterlockedDecrementLong(long *, void *);
__declspec(dllimport)   int             __stdcall   Exi386InterlockedIncrementLong(long *);
__declspec(dllimport)   int             __stdcall   Exi386InterlockedDecrementLong(long *);

#include <kmt_test.h>

/* TODO: There are quite some changes needed for other architectures!
         ExInterlockedAddLargeInteger, ExInterlockedAddUlong are the only two
         functions actually exported by my win7/x64 kernel! */

/* TODO: stress-testing */

static KSPIN_LOCK SpinLock;

#ifdef _M_IX86
typedef struct
{
    unsigned long esi, edi, ebx, ebp, esp;
} PROCESSOR_STATE;
#elif defined(_M_AMD64)
typedef struct
{
    unsigned long long rsi, rdi, rbx, rbp, rsp, r12, r13, r14, r15;
} PROCESSOR_STATE;
#else
// dummy
typedef int PROCESSOR_STATE;
#endif

/* TODO: these need to be rewritten in proper assembly to account for registers
 *       saved by the caller */
#if defined(_MSC_VER) && defined(_M_IX86)
#define SaveState(State) do                                                 \
{                                                                           \
    __asm lea ecx,      [State]                                             \
    __asm mov [ecx],    esi                                                 \
    __asm mov [ecx+4],  edi                                                 \
    __asm mov [ecx+8],  ebx                                                 \
    __asm mov [ecx+12], ebp                                                 \
    __asm mov [ecx+16], esp                                                 \
} while (0)

#define CheckState(OldState, NewState) do                                   \
{                                                                           \
    /* TODO: MSVC uses esi and saves it before, so this is okay */          \
    /*ok_eq_hex((OldState)->esi, (NewState)->esi);*/                        \
    ok_eq_hex((OldState)->edi, (NewState)->edi);                            \
    ok_eq_hex((OldState)->ebx, (NewState)->ebx);                            \
    ok_eq_hex((OldState)->ebp, (NewState)->ebp);                            \
    ok_eq_hex((OldState)->esp, (NewState)->esp);                            \
} while (0)

#elif defined(__GNUC__) && defined(_M_IX86)
#define SaveState(State)                                                    \
    asm volatile(                                                           \
        "movl\t%%esi, (%%ecx)\n\t"                                          \
        "movl\t%%edi, 4(%%ecx)\n\t"                                         \
        "movl\t%%ebx, 8(%%ecx)\n\t"                                         \
        "movl\t%%ebp, 12(%%ecx)\n\t"                                        \
        "movl\t%%esp, 16(%%ecx)"                                            \
        : : "c" (&State) : "memory"                                         \
    );

#define CheckState(OldState, NewState) do                                   \
{                                                                           \
    /* TODO: GCC 4.7 uses esi and saves it before, so this is okay */       \
    /*ok_eq_hex((OldState)->esi, (NewState)->esi);*/                        \
    ok_eq_hex((OldState)->edi, (NewState)->edi);                            \
    /* TODO: GCC 4.4 uses ebx and saves it before, so this is okay */       \
    /*ok_eq_hex((OldState)->ebx, (NewState)->ebx);*/                        \
    ok_eq_hex((OldState)->ebp, (NewState)->ebp);                            \
    ok_eq_hex((OldState)->esp, (NewState)->esp);                            \
} while (0)
#elif defined(__GNUC__) && defined(_M_AMD64)
#define SaveState(State)                                                    \
    asm volatile(                                                           \
        "mov\t%%rsi, (%%rcx)\n\t"                                           \
        "mov\t%%rdi, 8(%%rcx)\n\t"                                          \
        "mov\t%%rbx, 16(%%rcx)\n\t"                                         \
        "mov\t%%rbp, 24(%%rcx)\n\t"                                         \
        "mov\t%%rsp, 32(%%rcx)\n\t"                                         \
        "mov\t%%r12, 40(%%rcx)\n\t"                                         \
        "mov\t%%r13, 48(%%rcx)\n\t"                                         \
        "mov\t%%r14, 56(%%rcx)\n\t"                                         \
        "mov\t%%r15, 64(%%rcx)"                                             \
        : : "c" (&State) : "memory"                                         \
    );

#define CheckState(OldState, NewState) do                                   \
{                                                                           \
    ok_eq_hex((OldState)->rsi, (NewState)->rsi);                            \
    ok_eq_hex((OldState)->rdi, (NewState)->rdi);                            \
    ok_eq_hex((OldState)->rbx, (NewState)->rbx);                            \
    ok_eq_hex((OldState)->rbp, (NewState)->rbp);                            \
    ok_eq_hex((OldState)->rsp, (NewState)->rsp);                            \
    ok_eq_hex((OldState)->r12, (NewState)->r12);                            \
    ok_eq_hex((OldState)->r13, (NewState)->r13);                            \
    ok_eq_hex((OldState)->r14, (NewState)->r14);                            \
    ok_eq_hex((OldState)->r15, (NewState)->r15);                            \
} while (0)
#else
#define SaveState(State)
#define CheckState(OldState, NewState) do                                   \
{                                                                           \
    (void)OldState;                                                         \
    (void)NewState;                                                         \
} while (0)
#endif

static
LARGE_INTEGER
Large(
    ULONGLONG Value)
{
    LARGE_INTEGER Ret;
    Ret.QuadPart = Value;
    return Ret;
}

#define CheckInterlockedCmpXchg(Function, Type, Print, Val, Cmp, Xchg,      \
                                ExpectedValue, ExpectedRet) do              \
{                                                                           \
    Type Ret##Type = 0;                                                     \
    Type Value##Type = Val;                                                 \
    Status = STATUS_SUCCESS;                                                \
    _SEH2_TRY {                                                             \
        SaveState(OldState);                                                \
        Ret##Type = Function(&Value##Type, Xchg, Cmp);                      \
        SaveState(NewState);                                                \
        CheckState(&OldState, &NewState);                                   \
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {                             \
        Status = _SEH2_GetExceptionCode();                                  \
    } _SEH2_END;                                                            \
    ok_eq_hex(Status, STATUS_SUCCESS);                                      \
    ok_eq_print(Ret##Type, ExpectedRet, Print);                             \
    ok_eq_print(Value##Type, ExpectedValue, Print);                         \
} while (0)

#define CheckInterlockedCmpXchgI(Function, Type, Print, Val, Cmp, Xchg,     \
                                ExpectedValue, ExpectedRet, ...) do         \
{                                                                           \
    Type Ret##Type = 0;                                                     \
    Type Value##Type = Val;                                                 \
    Type Compare##Type = Cmp;                                               \
    Type Exchange##Type = Xchg;                                             \
    Status = STATUS_SUCCESS;                                                \
    _SEH2_TRY {                                                             \
        SaveState(OldState);                                                \
        Ret##Type = Function(&Value##Type, &Exchange##Type,                 \
                                &Compare##Type, ##__VA_ARGS__);             \
        SaveState(NewState);                                                \
        CheckState(&OldState, &NewState);                                   \
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {                             \
        Status = _SEH2_GetExceptionCode();                                  \
    } _SEH2_END;                                                            \
    ok_eq_hex(Status, STATUS_SUCCESS);                                      \
    ok_eq_print(Ret##Type, ExpectedRet, Print);                             \
    ok_eq_print(Value##Type, ExpectedValue, Print);                         \
    ok_eq_print(Exchange##Type, Xchg, Print);                               \
    ok_eq_print(Compare##Type, Cmp, Print);                                 \
} while(0)

#define CheckInterlockedOp(Function, Type, Print, Val, Op,                  \
                                ExpectedValue, ExpectedRet, ...) do         \
{                                                                           \
    Type Ret##Type = 0;                                                     \
    Type Value##Type = Val;                                                 \
    Status = STATUS_SUCCESS;                                                \
    _SEH2_TRY {                                                             \
        SaveState(OldState);                                                \
        Ret##Type = Function(&Value##Type, Op, ##__VA_ARGS__);              \
        SaveState(NewState);                                                \
        CheckState(&OldState, &NewState);                                   \
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {                             \
        Status = _SEH2_GetExceptionCode();                                  \
    } _SEH2_END;                                                            \
    ok_eq_hex(Status, STATUS_SUCCESS);                                      \
    ok_eq_print(Ret##Type, ExpectedRet, Print);                             \
    ok_eq_print(Value##Type, ExpectedValue, Print);                         \
} while (0)

#define CheckInterlockedOpNoArg(Function, Type, Print, Val,                 \
                                ExpectedValue, ExpectedRet, ...) do         \
{                                                                           \
    Type Ret##Type = 0;                                                     \
    Type Value##Type = Val;                                                 \
    Status = STATUS_SUCCESS;                                                \
    _SEH2_TRY {                                                             \
        SaveState(OldState);                                                \
        Ret##Type = Function(&Value##Type, ##__VA_ARGS__);                  \
        SaveState(NewState);                                                \
        CheckState(&OldState, &NewState);                                   \
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {                             \
        Status = _SEH2_GetExceptionCode();                                  \
    } _SEH2_END;                                                            \
    ok_eq_hex(Status, STATUS_SUCCESS);                                      \
    ok_eq_print(Ret##Type, ExpectedRet, Print);                             \
    ok_eq_print(Value##Type, ExpectedValue, Print);                         \
} while (0)

#define CheckInterlockedOpLarge(Function, Type, Print, Val, Op,             \
                                ExpectedValue, ExpectedRet, ...) do         \
{                                                                           \
    Type Ret##Type = Large(0);                                              \
    Type Value##Type = Val;                                                 \
    Status = STATUS_SUCCESS;                                                \
    _SEH2_TRY {                                                             \
        SaveState(OldState);                                                \
        Ret##Type = Function(&Value##Type, Op, ##__VA_ARGS__);              \
        SaveState(NewState);                                                \
        CheckState(&OldState, &NewState);                                   \
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {                             \
        Status = _SEH2_GetExceptionCode();                                  \
    } _SEH2_END;                                                            \
    ok_eq_hex(Status, STATUS_SUCCESS);                                      \
    ok_eq_print(Ret##Type.QuadPart, ExpectedRet, Print);                    \
    ok_eq_print(Value##Type.QuadPart, ExpectedValue, Print);                \
} while (0)

#define CheckInterlockedOpLargeNoRet(Function, Type, Print, Val, Op,        \
                                ExpectedValue) do                           \
{                                                                           \
    Type Value##Type = Val;                                                 \
    Status = STATUS_SUCCESS;                                                \
    _SEH2_TRY {                                                             \
        SaveState(OldState);                                                \
        Function(&Value##Type, Op);                                         \
        SaveState(NewState);                                                \
        CheckState(&OldState, &NewState);                                   \
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {                             \
        Status = _SEH2_GetExceptionCode();                                  \
    } _SEH2_END;                                                            \
    ok_eq_hex(Status, STATUS_SUCCESS);                                      \
    ok_eq_print(Value##Type.QuadPart, ExpectedValue, Print);                \
} while (0)

static
VOID
TestInterlockedFunctional(VOID)
{
    NTSTATUS Status;
    PKSPIN_LOCK pSpinLock = &SpinLock;<--- pSpinLock is initialized
    PROCESSOR_STATE OldState, NewState;

    /* on x86, most of these are supported intrinsically and don't need a spinlock! */
#if defined _M_IX86 || defined _M_AMD64
    pSpinLock = NULL;<--- pSpinLock is overwritten
#endif

    /* CompareExchange */
    /* macro version */
    CheckInterlockedCmpXchg(InterlockedCompareExchange, LONG, "%ld", 5, 6, 8, 5L, 5L);
    CheckInterlockedCmpXchg(InterlockedCompareExchange, LONG, "%ld", 5, 5, 9, 9L, 5L);
    /* these only exist as macros on x86 */
    CheckInterlockedCmpXchg(InterlockedCompareExchangeAcquire, LONG, "%ld", 16, 9, 12, 16L, 16L);
    CheckInterlockedCmpXchg(InterlockedCompareExchangeAcquire, LONG, "%ld", 16, 16, 4, 4L, 16L);
    CheckInterlockedCmpXchg(InterlockedCompareExchangeRelease, LONG, "%ld", 27, 123, 38, 27L, 27L);
    CheckInterlockedCmpXchg(InterlockedCompareExchangeRelease, LONG, "%ld", 27, 27, 39, 39L, 27L);
    /* exported function */
#undef InterlockedCompareExchange
#ifdef _M_IX86
    CheckInterlockedCmpXchg(InterlockedCompareExchange, LONG, "%ld", 5, 6, 8, 5L, 5L);
    CheckInterlockedCmpXchg(InterlockedCompareExchange, LONG, "%ld", 5, 5, 9, 9L, 5L);
#endif
    /* only exists as a macro */
    CheckInterlockedCmpXchg(InterlockedCompareExchangePointer, PVOID, "%p", (PVOID)117, (PVOID)711, (PVOID)12, (PVOID)117, (PVOID)117);
    CheckInterlockedCmpXchg(InterlockedCompareExchangePointer, PVOID, "%p", (PVOID)117, (PVOID)117, (PVOID)228, (PVOID)228, (PVOID)117);
    /* macro version */
    CheckInterlockedCmpXchgI(ExInterlockedCompareExchange64, LONGLONG, "%I64d", 17, 4LL, 20LL, 17LL, 17LL, pSpinLock);
    CheckInterlockedCmpXchgI(ExInterlockedCompareExchange64, LONGLONG, "%I64d", 17, 17LL, 21LL, 21LL, 17LL, pSpinLock);
#ifdef _M_IX86
    /* exported function */
    CheckInterlockedCmpXchgI((ExInterlockedCompareExchange64), LONGLONG, "%I64d", 17, 4LL, 20LL, 17LL, 17LL, pSpinLock);
    CheckInterlockedCmpXchgI((ExInterlockedCompareExchange64), LONGLONG, "%I64d", 17, 17LL, 21LL, 21LL, 17LL, pSpinLock);
    /* fastcall version */
    CheckInterlockedCmpXchgI(ExfInterlockedCompareExchange64, LONGLONG, "%I64d", 17, 4LL, 20LL, 17LL, 17LL);
    CheckInterlockedCmpXchgI(ExfInterlockedCompareExchange64, LONGLONG, "%I64d", 17, 17LL, 21LL, 21LL, 17LL);
#endif

    /* Exchange */
    CheckInterlockedOp(InterlockedExchange, LONG, "%ld", 5, 8, 8L, 5L);
    CheckInterlockedOpNoArg(InterlockedExchangePointer, PVOID, "%p", (PVOID)700, (PVOID)93, (PVOID)700, (PVOID)93);
#undef InterlockedExchange
#ifdef _M_IX86
    CheckInterlockedOp(InterlockedExchange, LONG, "%ld", 5, 8, 8L, 5L);
    CheckInterlockedOp(ExInterlockedExchangeUlong, ULONG, "%lu", 212, 121, 121LU, 212LU, pSpinLock);
    CheckInterlockedOp((ExInterlockedExchangeUlong), ULONG, "%lu", 212, 121, 121LU, 212LU, pSpinLock);
    CheckInterlockedOp(Exi386InterlockedExchangeUlong, ULONG, "%lu", 212, 121, 121LU, 212LU);
    CheckInterlockedOp(Exfi386InterlockedExchangeUlong, ULONG, "%lu", 212, 121, 121LU, 212LU);
#endif

    /* ExchangeAdd */
    /* TODO: ExInterlockedExchangeAddLargeInteger? */
    CheckInterlockedOp(InterlockedExchangeAdd, LONG, "%ld", 312, 7, 319L, 312L);
#undef InterlockedExchangeAdd
#ifdef _M_IX86
    CheckInterlockedOp(InterlockedExchangeAdd, LONG, "%ld", 312, 7, 319L, 312L);
#endif

    /* Add */
    /* these DO need a valid spinlock even on x86 */
    CheckInterlockedOpLarge(ExInterlockedAddLargeInteger, LARGE_INTEGER, "%I64d", Large(23), Large(7), 30LL, 23LL, &SpinLock);<--- RetLARGE_INTEGER is overwritten<--- RetLARGE_INTEGER is initialized
    CheckInterlockedOpLargeNoRet(ExInterlockedAddLargeStatistic, LARGE_INTEGER, "%I64d", Large(15), 17LL, 32LL);
    CheckInterlockedOp(ExInterlockedAddUlong, ULONG, "%lu", 239, 44, 283LU, 239LU, &SpinLock);
#undef ExInterlockedAddUlong
    CheckInterlockedOp(ExInterlockedAddUlong, ULONG, "%lu", 239, 44, 283LU, 239LU, &SpinLock);

    /* Increment */
    CheckInterlockedOpNoArg(InterlockedIncrement, LONG, "%ld", 2341L, 2342L, 2342L);
    CheckInterlockedOpNoArg(InterlockedIncrement, LONG, "%ld", (LONG)MAXLONG, (LONG)MINLONG, (LONG)MINLONG);
    CheckInterlockedOpNoArg(InterlockedIncrementAcquire, LONG, "%ld", 2341L, 2342L, 2342L);
    CheckInterlockedOpNoArg(InterlockedIncrementRelease, LONG, "%ld", 2341L, 2342L, 2342L);
#undef InterlockedIncrement
#ifdef _M_IX86
    CheckInterlockedOpNoArg(InterlockedIncrement, LONG, "%ld", 2341L, 2342L, 2342L);
    CheckInterlockedOpNoArg(InterlockedIncrement, LONG, "%ld", (LONG)MAXLONG, (LONG)MINLONG, (LONG)MINLONG);
    CheckInterlockedOpNoArg(ExInterlockedIncrementLong, LONG, "%ld", -2L, -1L, (LONG)ResultNegative, pSpinLock);
    CheckInterlockedOpNoArg(ExInterlockedIncrementLong, LONG, "%ld", -1L, 0L, (LONG)ResultZero, pSpinLock);
    CheckInterlockedOpNoArg(ExInterlockedIncrementLong, LONG, "%ld", 0L, 1L, (LONG)ResultPositive, pSpinLock);
    CheckInterlockedOpNoArg(ExInterlockedIncrementLong, LONG, "%ld", (LONG)MAXLONG, (LONG)MINLONG, (LONG)ResultNegative, pSpinLock);
    CheckInterlockedOpNoArg((ExInterlockedIncrementLong), LONG, "%ld", -2L, -1L, (LONG)ResultNegative, pSpinLock);
    CheckInterlockedOpNoArg((ExInterlockedIncrementLong), LONG, "%ld", -1L, 0L, (LONG)ResultZero, pSpinLock);
    CheckInterlockedOpNoArg((ExInterlockedIncrementLong), LONG, "%ld", 0L, 1L, (LONG)ResultPositive, pSpinLock);
    CheckInterlockedOpNoArg((ExInterlockedIncrementLong), LONG, "%ld", (LONG)MAXLONG, (LONG)MINLONG, (LONG)ResultNegative, pSpinLock);
    CheckInterlockedOpNoArg(Exi386InterlockedIncrementLong, LONG, "%ld", -2L, -1L, (LONG)ResultNegative);
    CheckInterlockedOpNoArg(Exi386InterlockedIncrementLong, LONG, "%ld", -1L, 0L, (LONG)ResultZero);
    CheckInterlockedOpNoArg(Exi386InterlockedIncrementLong, LONG, "%ld", 0L, 1L, (LONG)ResultPositive);
    CheckInterlockedOpNoArg(Exi386InterlockedIncrementLong, LONG, "%ld", (LONG)MAXLONG, (LONG)MINLONG, (LONG)ResultNegative);
#endif

    /* Decrement */
    CheckInterlockedOpNoArg(InterlockedDecrement, LONG, "%ld", 1745L, 1744L, 1744L);
    CheckInterlockedOpNoArg(InterlockedDecrement, LONG, "%ld", (LONG)MINLONG, (LONG)MAXLONG, (LONG)MAXLONG);
    CheckInterlockedOpNoArg(InterlockedDecrementAcquire, LONG, "%ld", 1745L, 1744L, 1744L);
    CheckInterlockedOpNoArg(InterlockedDecrementRelease, LONG, "%ld", 1745L, 1744L, 1744L);
#undef InterlockedDecrement
#ifdef _M_IX86
    CheckInterlockedOpNoArg(InterlockedDecrement, LONG, "%ld", 1745L, 1744L, 1744L);
    CheckInterlockedOpNoArg(InterlockedDecrement, LONG, "%ld", (LONG)MINLONG, (LONG)MAXLONG, (LONG)MAXLONG);
    CheckInterlockedOpNoArg(ExInterlockedDecrementLong, LONG, "%ld", (LONG)MINLONG, (LONG)MAXLONG, (LONG)ResultPositive, pSpinLock);
    CheckInterlockedOpNoArg(ExInterlockedDecrementLong, LONG, "%ld", 0L, -1L, (LONG)ResultNegative, pSpinLock);
    CheckInterlockedOpNoArg(ExInterlockedDecrementLong, LONG, "%ld", 1L, 0L, (LONG)ResultZero, pSpinLock);
    CheckInterlockedOpNoArg(ExInterlockedDecrementLong, LONG, "%ld", 2L, 1L, (LONG)ResultPositive, pSpinLock);
    CheckInterlockedOpNoArg((ExInterlockedDecrementLong), LONG, "%ld", (LONG)MINLONG, (LONG)MAXLONG, (LONG)ResultPositive, pSpinLock);
    CheckInterlockedOpNoArg((ExInterlockedDecrementLong), LONG, "%ld", 0L, -1L, (LONG)ResultNegative, pSpinLock);
    CheckInterlockedOpNoArg((ExInterlockedDecrementLong), LONG, "%ld", 1L, 0L, (LONG)ResultZero, pSpinLock);
    CheckInterlockedOpNoArg((ExInterlockedDecrementLong), LONG, "%ld", 2L, 1L, (LONG)ResultPositive, pSpinLock);
    CheckInterlockedOpNoArg(Exi386InterlockedDecrementLong, LONG, "%ld", (LONG)MINLONG, (LONG)MAXLONG, (LONG)ResultPositive);
    CheckInterlockedOpNoArg(Exi386InterlockedDecrementLong, LONG, "%ld", 0L, -1L, (LONG)ResultNegative);
    CheckInterlockedOpNoArg(Exi386InterlockedDecrementLong, LONG, "%ld", 1L, 0L, (LONG)ResultZero);
    CheckInterlockedOpNoArg(Exi386InterlockedDecrementLong, LONG, "%ld", 2L, 1L, (LONG)ResultPositive);
#endif

    /* And, Or, Xor */
    CheckInterlockedOp(InterlockedAnd, LONG, "0x%lx", 0x1234L, 0x1111L, 0x1010L, 0x1234L);
    CheckInterlockedOp(InterlockedOr, LONG, "0x%lx", 0x1234L, 0x1111L, 0x1335L, 0x1234L);
    CheckInterlockedOp(InterlockedXor, LONG, "0x%lx", 0x1234L, 0x1111L, 0x0325L, 0x1234L);
#ifdef _WIN64
    CheckInterlockedOp(InterlockedXor64, LONGLONG, "0x%I64x", 0x200001234LL, 0x100001111LL, 0x300000325LL, 0x200001234LL);
#endif
}

START_TEST(ExInterlocked)
{
    KIRQL Irql;
    KeInitializeSpinLock(&SpinLock);

    /* functional testing */
    TestInterlockedFunctional();
    KeRaiseIrql(HIGH_LEVEL, &Irql);
    TestInterlockedFunctional();
    KeLowerIrql(Irql);
}