aboutsummaryrefslogtreecommitdiff
path: root/CRT/DriverThread.cpp
blob: 3a77dc865e944d7759494f82cedbc8488cac831d (plain)
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
#include <DriverThread.hpp>

class WorkQueueArgs : public DriverThread::ThreadArgs
{
    friend class WorkQueue;

public:
    WorkQueueArgs(DriverThread::WorkQueue * wq) : m_wq(wq){};
    WorkQueueArgs(const WorkQueueArgs &) = delete;
    DriverThread::WorkQueue * getWorkQueue()
    {
        return m_wq;
    }

private:
    DriverThread::WorkQueue * m_wq;
};

// Thread

DriverThread::Thread::Thread(void)
{
}

DriverThread::Thread::~Thread(void)
{
    WaitForTermination();
}

extern "C" void InterceptorThreadRoutine(PVOID threadContext)
{
    NTSTATUS threadReturn;
    DriverThread::Thread * self = (DriverThread::Thread *)threadContext;

    self->m_threadId = PsGetCurrentThreadId();
    threadReturn = self->m_routine(self->m_threadContext);
    self->m_threadId = nullptr;
    self->m_threadContext = nullptr;
    PsTerminateSystemThread(threadReturn);
}

NTSTATUS DriverThread::Thread::Start(ThreadRoutine routine, eastl::shared_ptr<ThreadArgs> args)
{
    HANDLE threadHandle;
    NTSTATUS status;

    LockGuard lock(m_mutex);
    if (m_threadObject != nullptr)
    {
        return STATUS_UNSUCCESSFUL;
    }

    m_routine = routine;
    m_threadContext = args;
    status = PsCreateSystemThread(&threadHandle, (ACCESS_MASK)0, NULL, (HANDLE)0, NULL, InterceptorThreadRoutine, this);

    if (!NT_SUCCESS(status))
    {
        return status;
    }

    status =
        ObReferenceObjectByHandle(threadHandle, THREAD_ALL_ACCESS, NULL, KernelMode, (PVOID *)&m_threadObject, NULL);

    if (!NT_SUCCESS(status))
    {
        return status;
    }

    return ZwClose(threadHandle);
}

NTSTATUS DriverThread::Thread::WaitForTermination(LONGLONG timeout)
{
    if (PsGetCurrentThreadId() == m_threadId)
    {
        return STATUS_UNSUCCESSFUL;
    }

    LockGuard lock(m_mutex);
    if (m_threadObject == nullptr)
    {
        return STATUS_UNSUCCESSFUL;
    }

    LARGE_INTEGER li_timeout = {.QuadPart = timeout};
    NTSTATUS status =
        KeWaitForSingleObject(m_threadObject, Executive, KernelMode, FALSE, (timeout == 0 ? NULL : &li_timeout));

    ObDereferenceObject(m_threadObject);
    m_threadObject = nullptr;
    return status;
}

// Spinlock

DriverThread::Spinlock::Spinlock(void)
{
    KeInitializeSpinLock(&m_spinLock);
}

NTSTATUS DriverThread::Spinlock::Acquire(void)
{
    return KeAcquireSpinLock(&m_spinLock, &m_oldIrql);
}

void DriverThread::Spinlock::Release(void)
{
    KeReleaseSpinLock(&m_spinLock, m_oldIrql);
}

KIRQL DriverThread::Spinlock::GetOldIrql(void)
{
    return m_oldIrql;
}

// Semaphore

DriverThread::Semaphore::Semaphore(LONG initialValue, LONG maxValue)
{
    KeInitializeSemaphore(&m_semaphore, initialValue, maxValue);
}

NTSTATUS DriverThread::Semaphore::Wait(LONGLONG timeout)
{
    LARGE_INTEGER li_timeout = {.QuadPart = timeout};
    return KeWaitForSingleObject(&m_semaphore, Executive, KernelMode, FALSE, (timeout == 0 ? NULL : &li_timeout));
}

LONG DriverThread::Semaphore::Release(LONG adjustment)
{
    return KeReleaseSemaphore(&m_semaphore, 0, adjustment, FALSE);
}

// Event

DriverThread::Event::Event()
{
    KeInitializeEvent(&m_event, NotificationEvent, FALSE);
}

NTSTATUS DriverThread::Event::Wait(LONGLONG timeout)
{
    LARGE_INTEGER li_timeout = {.QuadPart = timeout};
    return KeWaitForSingleObject(&m_event, Executive, KernelMode, FALSE, (timeout == 0 ? NULL : &li_timeout));
}

NTSTATUS DriverThread::Event::Notify()
{
    return KeSetEvent(&m_event, 0, FALSE);
}

// Mutex

DriverThread::Mutex::Mutex(void)
{
}

DriverThread::Mutex::~Mutex(void)
{
}

void DriverThread::Mutex::Lock(void)
{
    while (m_interlock == 1 || InterlockedCompareExchange(&m_interlock, 1, 0) == 1) {}
}

void DriverThread::Mutex::Unlock(void)
{
    m_interlock = 0;
}

// LockGuard

DriverThread::LockGuard::LockGuard(Mutex & m) : m_Lock(m)
{
    m_Lock.Lock();
}

DriverThread::LockGuard::~LockGuard(void)
{
    m_Lock.Unlock();
}

// WorkQueue

DriverThread::WorkQueue::WorkQueue(void)
    : m_mutex(), m_queue(), m_wakeEvent(), m_stopWorker(false), m_worker(), m_workerRoutine(nullptr)
{
}

DriverThread::WorkQueue::~WorkQueue(void)
{
    Stop();
}

NTSTATUS DriverThread::WorkQueue::Start(WorkerRoutine routine)
{
    NTSTATUS status;

    {
        LockGuard lock(m_mutex);
        m_workerRoutine = routine;
        auto wqa = eastl::make_shared<WorkQueueArgs>(this);
        status = m_worker.Start(WorkerInterceptorRoutine, wqa);
    }

    if (!NT_SUCCESS(status) && status != STATUS_UNSUCCESSFUL)
    {
        Stop();
    }

    return status;
}

void DriverThread::WorkQueue::Stop(bool wait)
{
    LockGuard lock(m_mutex);
    if (m_stopWorker == true)
    {
        return;
    }
    m_stopWorker = true;
    m_wakeEvent.Notify();
    if (wait)
    {
        m_worker.WaitForTermination();
    }
}

void DriverThread::WorkQueue::Enqueue(WorkItem & item)
{
    {
        LockGuard lock(m_mutex);
        m_queue.emplace_back(item);
    }
    m_wakeEvent.Notify();
}

void DriverThread::WorkQueue::Enqueue(eastl::deque<WorkItem> & items)
{
    {
        LockGuard lock(m_mutex);
        m_queue.insert(m_queue.end(), items.begin(), items.end());
    }
    m_wakeEvent.Notify();
}

NTSTATUS DriverThread::WorkQueue::WorkerInterceptorRoutine(eastl::shared_ptr<ThreadArgs> args)
{
    auto wqa = eastl::static_pointer_cast<WorkQueueArgs>(args);
    WorkQueue * wq = wqa->getWorkQueue();

    PAGED_CODE();

    for (;;)
    {
        eastl::deque<WorkItem> doQueue;
        std::size_t nItems;

        {
            LockGuard lock(wq->m_mutex);
            nItems = wq->m_queue.size();
        }

        if (nItems == 0)
        {
            if (wq->m_stopWorker == true)
            {
                break;
            }

            wq->m_wakeEvent.Wait();
            continue;
        }

        {
            LockGuard lock(wq->m_mutex);
            doQueue = wq->m_queue;
            wq->m_queue.clear();
        }

        while (doQueue.size() > 0)
        {
            WorkItem & item = doQueue.front();

            if (wq->m_workerRoutine(item) != STATUS_SUCCESS)
            {
                wq->m_stopWorker = true;
            }

            doQueue.pop_front();
        }
    }

    return STATUS_SUCCESS;
}