Deferred Procedure Calls
DPCs are the workhorses of NT's interrupt processing. NT tracks DPCs the
same way it tracks interrupts--in objects. Device drivers usually initialize a
DPC Object at the same time they connect to an interrupt. The information a
driver must specify includes the address of its DPC function, the processor the
DPC function needs to execute on, and the DPC's priority. By default, a DPC will
always execute on the processor the ISR executes on; however, a device driver
writer can override this assignment. In addition, DPCs default to medium
priority (the choices are low, medium, and high), but a device driver writer can
also control priority.
When an ISR requests a DPC, NT places the specified DPC Object on the
target processor's DPC queue. If the DPC has low or medium priority, NT places
the DPC Object at the end of the queue; if the DPC has high priority, NT inserts
the DPC Object at the front of the queue. When the processor's IRQL is about to
drop from Dispatch Level to a lower IRQL (APC Level or Passive Level), NT
removes the DPC Objects from the DPC queue. NT ensures that the IRQL remains at
Dispatch Level and pulls DPC Objects off the queue until the queue is empty
(i.e., NT drains the queue), calling each DPC function in turn. Only when the
queue is empty will NT let the IRQL drop below Dispatch Level and let regular
thread execution continue.
DPC priorities can affect system behavior another way. NT usually
initiates DPC queue draining with a software interrupt whose associated IRQL is
Dispatch Level. NT generates such an interrupt only if the DPC is directed at
the processor the ISR is requested on, and the DPC has high or medium priority.
If the DPC has low priority, the DPC requests the interrupt only if the number of outstanding DPC requests for the processor rises above a threshold, or the number of DPCs requested on the processor within a time window is low. If a DPC is targeted at a CPU different from the one the ISR is running on and the DPC's priority is high, NT immediately signals the target CPU to drain its DPC queue. If the priority is medium or low, the number of DPCs queued on the target processor must exceed a threshold. The system idle thread also drains the DPC queue. Table 2, page 56, summarizes the situations initiating DPC queue draining.
Figure 2 shows a typical sequence of events. First, an ISR requests a DPC,
and NT places the DPC Object on the DPC queue of the target processor. Depending
on the DPC priority and the length of the DPC queue, NT generates a DPC software
interrupt then or at some later time. When the processor drains the DPC queue,
the DPC Object leaves the queue and control transfers to its DPC function, which
completes interrupt processing by reading data from (or writing data to) the
device that originated the interrupt.
Interrupts and Scheduling
The IRQL information in Table 1, page 56, shows that Dispatch Level is
associated with scheduling operations. When the IRQL is at Dispatch Level or
higher, NT masks scheduler software interrupts, which means that NT effectively
turns off the scheduler. In fact, device drivers (and NT) must not perform
operations that require an immediate response by the scheduler when a processor
is at an IRQL greater than or equal to Dispatch Level. This restriction includes
doing anything that might indicate to NT that the current thread is giving up
the CPU to wait for some event to occur because that action would cause the
scheduler to find a new thread to execute. Another action that demands scheduler
intervention is a page fault. When a thread accesses virtual memory that
references data in the paging file, NT usually blocks the thread until the data
is read. Therefore, at Dispatch Level or higher, NT does not permit access to
memory not locked into the CPU's physical memory. If you've seen the IRQL_
NOT_LESS_OR_EQUAL blue screen stop code, you've probably witnessed the effect of
a driver violating these rules.
Disabling the scheduler during interrupt processing has another,
less-obvious effect: NT counts the time taken by ISRs and DPC functions against
the quantum of the thread active at the time the CPU receives an interrupt. For
instance, suppose Word is executing a spell-check operation, an interrupt comes
in from a device, and the device's driver has a DPC that takes all the remaining
time in Word's quantum (and then some). When the CPU's IRQL drops below Dispatch
Level, the scheduler may decide to switch to a different thread in another
application, effectively penalizing Word for the interrupt processing. Although
this practice sounds unjust, most of the time NT distributes interrupt loads
evenly across the applications in a system.
NT as a Realtime Operating System
Deadline requirements, either hard or soft, characterize realtime
environments. Hard realtime systems (e.g., a nuclear power plant control system)
have deadlines that the system must meet to avoid catastrophic failures such as
loss of equipment or life. Soft realtime systems (e.g., a car's fuel-economy
optimization system) have deadlines that the system can miss, but have
timeliness as a desirable trait. In realtime systems, computers have sensor
input devices and control output devices. The designer of a realtime computer
system must know worst-case delays between the time an input device generates an
interrupt and the time that the device's driver can control the output device to
respond. This worst-case analysis must take into account the delays the
operating system introduces, as well as the delays the application and device
drivers impose.
Because NT does not prioritize device interrupts in any controllable way
and user-level applications execute only when a processor's IRQL is Passive
Level, NT is not always suitable as a realtime operating system. The system's
devices and device drivers--not NT--ultimately determine the worst-case delay
(the time from when an input device interrupts through when a realtime
application processes the input and controls the output device). This factor
becomes a problem when the designer of the realtime system uses off-the-shelf
hardware. The designer can have difficulty determining how long every
off-the-shelf device's ISR or DPC might take in the worst case. Even after
testing, the designer cannot guarantee that a special case in a live system will
not cause the system to miss an important deadline. Furthermore, the sum of all
the delays a system's DPCs and ISRs can introduce usually far exceeds the
tolerance of a time-sensitive environment.
Interrupt Management
Learning about how NT manages interrupts can help clear up confusion about
how NT's interrupt processing affects thread scheduling and other operations:
Thread priorities are essentially independent of interrupt priority levels. If
you are a systems programmer, information about NT's interrupt management can
help you understand how ISRs and DPCs fit into application processing. Finally,
if you are considering NT as an operating system for a realtime environment, you
can judge NT's suitability for your situation, or at least recognize some of the
issues you need to consider before making a decision.
there any improvements to the interrupt handling in Windows 2000 that would make it more suitable for real
time applications?
Paula Ward April 18, 2001