IRQL是Interrupt ReQuest Level,中斷請求級别。一個由windows虛拟出來的概念,劃分在windows下中斷的優先級,這裡中斷包括了硬中斷和軟中斷,硬中斷是由硬體産生,而軟中斷則是完全虛拟出來的。
處理器在一個IRQL上執行線程代碼。IRQL是幫助決定線程如何被中斷的。在同一處理器上,線程隻能被更進階别IRQL的線程能中斷。每個處理器都有自己的中斷IRQL。
我們在調用NDIS API時,在DDK幫助文檔中都有該API函數的所在級别。
PASSIVE_LEVEL
IRQL最低級别,沒有被屏蔽的中斷,在這個級别上,線程執行使用者模式,可以通路分頁記憶體。
APC_LEVEL
在這個級别上,隻有APC級别的中斷被屏蔽,可以通路分頁記憶體。當有APC發生時,處理器提升到APC級别,這樣,就屏蔽掉其它APC,為了和APC執行一些同步,驅動程式可以手動提升到這個級别。比如,如果提升到這個級别,APC就不能調用。在這個級别,APC被禁止了,導緻禁止一些I/O完成APC,是以有一些API不能調用。
DISPATCH_LEVEL
這個級别,DPC 和更低的中斷被屏蔽,不能通路分頁記憶體,所有的被通路的記憶體不能分頁。因為隻能處理非分頁記憶體,是以在這個級别,能夠通路的Api大大減少。
DIRQL (Device IRQL)
一般的,更進階的驅動在這個級别上不處理IRQL,但是幾乎所有的中斷被屏蔽,這實際上是IRQL的一個範圍,這是一個決定某個驅動有更高的優先級的方法。
The IRQL at which a driver routine executes determines which kernel-mode driver support routines it can call. For example, some driver support routines require that the caller be running at IRQL = DISPATCH_LEVEL. Others cannot be called safely if the caller is running at any IRQL higher than PASSIVE_LEVEL.
Following is a list of IRQLs at which the most commonly implemented standard driver routines are called. The IRQLs are listed from lowest to highest priority.
- PASSIVE_LEVEL
-
Interrupts Masked Off — None.
Driver Routines Called at PASSIVE_LEVEL — DriverEntry, AddDevice, Reinitialize, Unload routines, most dispatch routines, driver-created threads, worker-thread callbacks.
APC_LEVEL -
Interrupts Masked Off — APC_LEVEL interrupts are masked off.
Driver Routines Called at APC_LEVEL — Some dispatch routines (see Dispatch Routines and IRQLs).
DISPATCH_LEVEL -
Interrupts Masked Off — DISPATCH_LEVEL and APC_LEVEL interrupts are masked off. Device, clock, and power failure interrupts can occur.
Driver Routines Called at DISPATCH_LEVEL — StartIo, AdapterControl, AdapterListControl, ControllerControl, IoTimer, Cancel(while holding the cancel spin lock), DpcForIsr, CustomTimerDpc, CustomDpc routines.
DIRQL -
Interrupts Masked Off — All interrupts at IRQL<= DIRQL of driver's interrupt object. Device interrupts with a higher DIRQL value can occur, along with clock and power failure interrupts.
Driver Routines Called at DIRQL — InterruptService, SynchCritSection routines.
The only difference between APC_LEVEL and PASSIVE_LEVEL is that a process executing at APC_LEVEL cannot get APC interrupts. But both IRQLs imply a thread context and both imply that the code can be paged out.
Lowest-level drivers process IRPs while running at one of three IRQLs:
-
PASSIVE_LEVEL, with no interrupts masked off on the processor, in the driver's Dispatch routine(s)
DriverEntry, AddDevice, Reinitialize, and Unload routines also are run at PASSIVE_LEVEL, as are any driver-created system threads.
-
DISPATCH_LEVEL, with DISPATCH_LEVEL and APC_LEVEL interrupts masked off on the processor, in the StartIoroutine
AdapterControl, AdapterListControl, ControllerControl, IoTimer, Cancel (while it holds the cancel spin lock), andCustomTimerDpc routines also are run at DISPATCH_LEVEL, as are DpcForIsr and CustomDpc routines.
- Device IRQL (DIRQL), with all interrupts at less than or equal to the SynchronizeIrql of the driver's interrupt object(s) masked off on the processor, in the ISR and SynchCritSection routines
Most higher-level drivers process IRPs while running at either of two IRQLs:
-
PASSIVE_LEVEL, with no interrupts masked off on the processor, in the driver's dispatch routines
DriverEntry, Reinitialize, AddDevice, and Unload routines also are run at PASSIVE_LEVEL, as are any driver-created system threads or worker-thread callback routines or file system drivers.
-
DISPATCH_LEVEL, with DISPATCH_LEVEL and APC_LEVEL interrupts masked off on the processor, in the driver'sIoCompletion routine(s)
IoTimer, Cancel, and CustomTimerDpc routines also are run at DISPATCH_LEVEL.
In some circumstances, intermediate and lowest-level drivers of mass-storage devices are called at IRQL APC_LEVEL. In particular, this can occur at a page fault for which a file system driver sends an IRP_MJ_READ request to lower drivers.
Most standard driver routines are run at an IRQL that allows them simply to call the appropriate support routines. For example, a device driver must call AllocateAdapterChannel while running at IRQL DISPATCH_LEVEL. Since most device drivers call these routines from a StartIo routine, usually they are running at DISPATCH_LEVEL already.
Note that a device driver that has no StartIo routine because it sets up and manages its own queues of IRPs is not necessarily running at DISPATCH_LEVEL IRQL when it should call AllocateAdapterChannel. Such a driver must nest its call toAllocateAdapterChannel between calls to KeRaiseIrql and KeLowerIrql so that it runs at the required IRQL when it callsAllocateAdapterChannel and restores the original IRQL when the calling routine regains control.
When calling driver support routines, be aware of the following.
- Calling KeRaiseIrql with an input NewIrql value that is less than the current IRQL causes a fatal error. CallingKeLowerIrql except to restore the original IRQL (that is, after a call to KeRaiseIrql) also causes a fatal error.
- While running at IRQL >= DISPATCH_LEVEL, calling KeWaitForSingleObject or KeWaitForMultipleObjects for kernel-defined dispatcher objects to wait for a nonzero interval causes a fatal error.
- The only driver routines that can safely wait for events, semaphores, mutexes, or timers to be set to the signaled state are those that run in a nonarbitrary thread context at IRQL PASSIVE_LEVEL, such as driver-created threads, the DriverEntry and Reinitialize routines, or dispatch routines for inherently synchronous I/O operations (such as most device I/O control requests).
- Even while running at IRQL PASSIVE_LEVEL, pageable driver code must not call KeSetEvent, KeReleaseSemaphore, or KeReleaseMutex with the input Wait parameter set to TRUE. Such a call can cause a fatal page fault.
- Any routine that is running at greater than IRQL APC_LEVEL can neither allocate memory from paged pool nor access memory in paged pool safely. If a routine running at IRQL greater than APC_LEVEL causes a page fault, it is a fatal error.
-
A driver must be running at IRQL DISPATCH_LEVEL when it calls KeAcquireSpinLockAtDpcLevel andKeReleaseSpinLockFromDpcLevel.
A driver can be running at IRQL <= DISPATCH_LEVEL when it calls KeAcquireSpinLock but it must release that spin lock by calling KeReleaseSpinLock. In other words, it is a programming error to release a spin lock acquired withKeAcquireSpinLock by calling KeReleaseSpinLockFromDpcLevel.
A driver must not call KeAcquireSpinLockAtDpcLevel, KeReleaseSpinLockFromDpcLevel, KeAcquireSpinLock, orKeReleaseSpinLock while running at IRQL>DISPATCH_LEVEL.
- Calling a support routine that uses a spin lock, such as an ExInterlockedXxx routine, raises IRQL on the current processor either to DISPATCH_LEVEL or to DIRQL if the caller is not already running at a raised IRQL.
- Driver code that runs at IRQL > PASSIVE_LEVEL should execute as quickly as possible. The higher the IRQL at which a routine runs, the more important it is for good overall performance to tune that routine to execute as quickly as possible. For example, any driver that calls KeRaiseIrql should make the reciprocal call to KeLowerIrql as soon as it can.
For more information about determining priorities, see the Scheduling, Thread Context, and IRQL white paper that is available on the Microsoft Windows Hardware Developer Central (WHDC) Web site.