From 377ff4fb9b2ca6eb80cb6005d4bf438d88d8f0a6 Mon Sep 17 00:00:00 2001 From: Dibyamartanda Samanta Date: Fri, 1 Nov 2024 14:44:28 +0100 Subject: [PATCH] [NTOSKRNL:CC] Add Lock Guard for DPC Level SPinlock --- NTOSKRNL/CC/ccinternal.hpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/NTOSKRNL/CC/ccinternal.hpp b/NTOSKRNL/CC/ccinternal.hpp index 94a0e72..ee6e29b 100644 --- a/NTOSKRNL/CC/ccinternal.hpp +++ b/NTOSKRNL/CC/ccinternal.hpp @@ -105,6 +105,31 @@ private: KIRQL m_OldIrql; }; +class QueuedSpinlockAtDPC { +public: + // Constructor to acquire the lock + explicit QueuedSpinlockAtDPC(KSPIN_LOCK_QUEUE_NUMBER lockNumber) + : m_lockNumber(lockNumber) { + // Acquire the lock at DPC level + m_lockHandle = KeAcquireQueuedSpinLockAtDpcLevel(&KeGetCurrentPrcb()->LockQueue[m_lockNumber]); + } + + // Destructor to release the lock automatically + ~QueuedSpinlockAtDPC() { + // Release the lock at DPC level + KeReleaseQueuedSpinLockFromDpcLevel(&KeGetCurrentPrcb()->LockQueue[m_lockNumber], m_lockHandle); + } + + // Deleting copy and move constructors to prevent unintended copying + QueuedSpinlockAtDPC(const QueuedSpinlockAtDPC&) = delete; + QueuedSpinlockAtDPC& operator=(const QueuedSpinlockAtDPC&) = delete; + +private: + KSPIN_LOCK_QUEUE_NUMBER m_lockNumber; // Spinlock queue number + KIRQL m_lockHandle; // Lock handle returned by KeAcquireQueuedSpinLockAtDpcLevel +}; + + template class Array { public: