16.2.3 Calling Support Routines That Use Spin Locks

Calling KeAcquireSpinLock sets the IRQL on the current processor to DISPATCH_LEVEL until a corresponding KeReleaseSpinLock call restores the previous IRQL. Consequently, drivers must be executing at IRQL <= DISPATCH_LEVEL when they call the KeAcquireSpinLock.

Callers of KeAcquireSpinLockAtDpcLevel and KeReleaseSpinLockFromDpcLevel run faster because they are already running at IRQL DISPATCH_LEVEL so these support routines need not reset IRQL on the current processor. Consequently, it is a fatal error on most Windows NT platforms to call KeAcquireSpinLockAtDpcLevel while running at IRQL less than DISPATCH_LEVEL. It is also an error to release a spin lock that was acquired with KeAcquireSpinLock by calling KeReleaseSpinLockFromDpcLevel because the caller's original IRQL is not restored.

Routines that hold an executive spin lock, such as the ExInterlockedXxx, usually execute at IRQL DISPATCH_LEVEL until they release the spin lock and return control to the caller. However, it is possible for an NT driver's ISR and SynchCritSection routines to call certain ExInterlockedXxx, such as the ExInterlocked..List routines, as long as the spin lock passed to this set of ExInterlockedXxx is used exclusively by the ISR and SynchCritSection routines.

Each routine that holds an interrupt spin lock executes at the DIRQL of an associated set of interrupt objects. Therefore, a driver must not call KeAcquireSpinLock and KeReleaseSpinLock nor any other routine that uses an executive spin lock from its ISR or SynchCritSection routine(s). Such a call is an error that can cause a system deadlock, requiring the user to reboot his or her machine. Note that if a driver's ISR or SynchCritSection routine calls an ExInterlocked..List routine, such a driver cannot reuse the spin lock it passes to the ExInterlocked..List routines in calls to the Ke..SpinLock or Ke..SpinLock..DpcLevel support routines.

If a driver has a multivector ISR or more than one ISR, its routines can call KeSynchronizeExecution while executing at any IRQL up to the SynchronizeIrql value specified for the associated interrupt objects when they were connected. For more information about interrupt objects, see Chapter 3.

For more information about device drivers' SynchCritSection routines passed to KeSynchronizeExecution, see Chapters 4 and 10, and Section 16.2.4. For more information about managing hardware priorities in drivers, see Section 16.1, and see the Kernel-Mode Driver Reference for support-routine-specific IRQL requirements.