aboutsummaryrefslogtreecommitdiffstats
path: root/driver/interlocked.h
blob: a8f1be5b14ced4134ae2b6d42e6823637cb25691 (plain) (blame)
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
/* SPDX-License-Identifier: GPL-2.0
 *
 * Copyright (C) 2015-2021 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 */

#pragma once

#include <ntifs.h> /* Must be included before <wdm.h> */
#include <wdm.h>
#include <fltkernel.h>

#pragma warning(suppress : 28194) /* `Value` is aliased in WritePointerNoFence. */
static inline VOID
__WritePointerNoFence(_Out_ _Interlocked_operand_ PVOID volatile *Destination, _In_opt_ __drv_aliasesMem PVOID Value)
{
    _Analysis_assume_(Value); /* The _In_ should be an _In_opt_. */
    WritePointerNoFence(Destination, Value);
}
/* Suppresses warning about strict type matches, which don't quite make sense in this context. */
#define WritePointerNoFence(P, V) __WritePointerNoFence((PVOID *)(P), V)

#pragma warning(suppress : 28194) /* `Value` is aliased in WritePointerRelease. */
static inline VOID
__WritePointerRelease(_Out_ _Interlocked_operand_ PVOID volatile *Destination, _In_opt_ __drv_aliasesMem PVOID Value)
{
    _Analysis_assume_(Value); /* The _In_ should be an _In_opt_. */
    WritePointerRelease(Destination, Value);
}
/* Suppresses warning about strict type matches, which don't quite make sense in this context. */
#define WritePointerRelease(P, V) __WritePointerRelease((PVOID *)(P), V)

static inline VOID WriteMemoryBarrier(VOID)
{
#if defined(_ARM64_)
    __dmb(_ARM64_BARRIER_ISHST);
#elif defined(_ARM_)
    __dmb(_ARM_BARRIER_ISHST);
#elif defined(_AMD64_) || defined(_X86_)
    /* Strong ordering on Intel */
#else
#    error "Unknown arch. Consult smp_wmb."
#endif
    _ReadWriteBarrier();
}

#ifdef _WIN64
#    define InterlockedBitTestAndSetPtr(Addr, Nr) InterlockedBitTestAndSet64(Addr, Nr)
#else
#    define InterlockedBitTestAndSetPtr(Addr, Nr) InterlockedBitTestAndSet(Addr, Nr)
#endif

#ifndef InterlockedExchangePointerRelease
_Ret_writes_(_Inexpressible_(Unknown)) static inline PVOID InterlockedExchangePointerRelease(
    _Inout_ _At_(
        *Target,
        _Pre_writable_byte_size_(_Inexpressible_(Unknown)) _Post_writable_byte_size_(_Inexpressible_(Unknown)))
        _Interlocked_operand_ PVOID volatile *Target,
    _In_opt_ PVOID Value)
{
#    if defined(_ARM64_)
    __dmb(_ARM64_BARRIER_ISH);
#    elif defined(_ARM_)
    __dmb(_ARM_BARRIER_ISH);
#    elif defined(_AMD64_) || defined(_X86_)
    /* Atomic instructions are already serializing on Intel */
#    else
#        error "Unknown arch. Consult __atomic_release_fence/smp_mb__before_atomic."
#    endif
    return InterlockedExchangePointer(Target, Value);
}
#endif

_Must_inspect_result_
static inline BOOLEAN
InterlockedIncrementUnless(_Inout_ _Interlocked_operand_ LONG volatile *Destination, _In_ LONG Unless)
{
    for (LONG C = ReadNoFence(Destination), X;; C = X)
    {
        if (C == Unless)
            return FALSE;
        X = InterlockedCompareExchange(Destination, C + 1, C);
        if (X == C)
            return TRUE;
    }
}

_Must_inspect_result_
static inline BOOLEAN
InterlockedIncrementUnless64(_Inout_ _Interlocked_operand_ LONG64 volatile *Destination, _In_ LONG64 Unless)
{
    for (LONG64 C = ReadNoFence64(Destination), X;; C = X)
    {
        if (C == Unless)
            return FALSE;
        X = InterlockedCompareExchange64(Destination, C + 1, C);
        if (X == C)
            return TRUE;
    }
}

typedef LONG64 KREF;

static inline VOID
KrefInit(_Out_ KREF *Kref)
{
    WriteRaw64(Kref, 1);
}

static inline VOID
KrefGet(_Inout_ KREF *Kref)
{
    InterlockedIncrement64(Kref);
}

_Must_inspect_result_
static inline BOOLEAN
KrefGetUnlessZero(_Inout_ KREF *Kref)
{
    return InterlockedIncrementUnless64(Kref, 0);
}

static inline BOOLEAN
KrefPut(_Inout_ KREF *Kref, _In_ VOID (*Release)(_In_ KREF *Kref))
{
    if (!InterlockedDecrement64(Kref))
    {
        Release(Kref);
        return TRUE;
    }
    return FALSE;
}

_IRQL_requires_max_(APC_LEVEL)
static inline VOID
MuInitializePushLock(_Out_ PEX_PUSH_LOCK PushLock)
{
#ifdef EX_LEGACY_PUSH_LOCKS
    FltInitializePushLock(PushLock);
#else
    ExInitializePushLock(PushLock);
#endif
}

_Acquires_lock_(_Global_critical_region_)
_IRQL_requires_max_(APC_LEVEL)
static inline VOID
MuAcquirePushLockExclusive(_Inout_ _Requires_lock_not_held_(*_Curr_) _Acquires_exclusive_lock_(*_Curr_)
                               PEX_PUSH_LOCK PushLock)
{
#ifdef EX_LEGACY_PUSH_LOCKS
    FltAcquirePushLockExclusive(PushLock);
#else
    KeEnterCriticalRegion();
    ExAcquirePushLockExclusive(PushLock);
#endif
}

_Acquires_lock_(_Global_critical_region_)
_IRQL_requires_max_(APC_LEVEL)
static inline VOID
MuAcquirePushLockShared(_Inout_ _Requires_lock_not_held_(*_Curr_) _Acquires_shared_lock_(*_Curr_)
                            PEX_PUSH_LOCK PushLock)
{
#ifdef EX_LEGACY_PUSH_LOCKS
    FltAcquirePushLockShared(PushLock);
#else
    KeEnterCriticalRegion();
    ExAcquirePushLockShared(PushLock);
#endif
}

_Releases_lock_(_Global_critical_region_)
_IRQL_requires_max_(APC_LEVEL)
static inline VOID
MuReleasePushLockExclusive(_Inout_ _Requires_exclusive_lock_held_(*_Curr_) _Releases_exclusive_lock_(*_Curr_)
                               PEX_PUSH_LOCK PushLock)
{
#ifdef EX_LEGACY_PUSH_LOCKS
    FltReleasePushLock(PushLock);
#else
    ExReleasePushLockExclusive(PushLock);
    KeLeaveCriticalRegion();
#endif
}

_Releases_lock_(_Global_critical_region_)
_IRQL_requires_max_(APC_LEVEL)
static inline VOID
MuReleasePushLockShared(_Inout_ _Requires_shared_lock_held_(*_Curr_) _Releases_shared_lock_(*_Curr_)
                            PEX_PUSH_LOCK PushLock)
{
    _Analysis_suppress_lock_checking_(PushLock);
#ifdef EX_LEGACY_PUSH_LOCKS
    FltReleasePushLock(PushLock);
#else
    ExReleasePushLockShared(PushLock);
    KeLeaveCriticalRegion();
#endif
}