IO Request Flow in WDF KernelMode Drivers Peter























- Slides: 23

I/O Request Flow in WDF Kernel‑Mode Drivers Peter G. Viscarola, Open Systems Resources, Inc. Microsoft DDK Most Valuable Professional May 10, 2006

Introduce to o o This paper describes how I/O requests are processed within the Microsoft® Windows® operating system and the Windows Driver Foundation (WDF) kernel-mode driver framework (KMDF) I/O flow is a complex topic, so this paper is organized into two parts n n focusing on the general activities performed by each component that handles the request focusing on the path of the request through different routines in the driver according to the options and configurations the driver chooses

The Flow of an I/O Request Through the System o Application I/O Requests n n Common I/O Requests Issued by Applications o Create Requests o Close Requests o Read and Write Requests o Device Control Requests Synchronous and Asynchronous Application Requests o Are Asynchronous Requests Always Completed Asynchronously? o Are Synchronous Requests Always Completed Synchronously?

I/O Request Path from Application to Driver 1. When an application issues an I/O request, Windows issues a system service call to the system service dispatcher. 2. The system service dispatcher calls the corresponding I/O processing function of the Windows I/O Manager. 3. The I/O Manager builds an I/O request packet (IRP) that describes the I/O request issued by the application and sends it to the framework (the KMDF library, Wdfdynam. sys). The framework sorts incoming requests by major function code and, in the case of a read, write, or device control request, sends it to the framework's I/O package for processing. The I/O package validates the request parameters and processes the request according to its type and the driver's configuration of its WDFQUEUE(s). 4. If the KMDF driver is a filter driver and has configured its WDFQUEUE(s) to forward requests of this type to the next-lower driver on the underlying driver stack, the framework forwards the request to the next-lower driver. Otherwise, it forwards the request to the KMDF driver. 5. If the KMDF driver receives the request, it processes the request and, if necessary, forwards it to the next-lower driver on the underlying stack. 6. That driver processes the request and, in turn, forwards the request to the next-lower driver and so on down the driver stack, as needed to satisfy the request.

I/O Request Path from Application to Driver (cont. ) o o o o I/O Request Path from Application to the Framework Windows Issues a System Service Call for an Application I/O Request The System Service Dispatcher Calls the I/O Manager The I/O Manager Builds an IRP and Sends It to the Framework Validating request parameters Building the IRP Sending the IRP to the framework.

I/O Request Path from Framework to Driver The KMDF Request Processing Pipeline

I/O Request Path from Framework to Driver (cont. ) o o The Framework Routes the Request to the Appropriate Package The I/O Package Checks the I/O Request Type The I/O Package Further Validates Request Parameters n Read and write requests n Device control requests The Framework Processes the Request n Handles the request on behalf of the driver n Forwards the requests to the driver for the next-lower device in the device stack. n Creates a WDFREQUEST object that represents the arriving request and passes it to the KMDF driver.

KMDF Driver Processing of I/O Requests o o o Processing a Request Internall Processing a Request by Sending It to Another Driver Processing before sending the request. Initializing the next I/O stack location in the request Sending requests synchronously or asynchronously

I/O Completion Path from Driver to Application

I/O Completion Path from Driver to Application (step A) o Driver Completion of a Request n o o o When an I/O request is fully satisfied I/O status. Completion information. Requested data.

I/O Completion Path from Driver to Application (step B) o I/O Completion Path from Framework to I/O Manager n n When a KMDF driver completes a WDFREQUEST, it calls a function such as Wdf. Request. Complete. Next, the framework destroys the WDFREQUEST itself, returning any context storage associated with the WDFREQUEST

I/O Completion Path from Driver to Application (step C) o o I/O Manager Completion of a Request The I/O Manager completes I/O requests in two stages: n n Stage 1: Completion processing that might take place in any process and thread context (sometimes called an "arbitrary" process and thread context). Stage 2: Completion processing that must take place in the context of the thread that issued the I/O request.

Flow of an I/O Request Within a KMDF Driver describes the path an I/O request takes during processing within a KMDF driver.

Factors that influence the path of an I/O request within a KMDF driver. o o The type of device being supported. The driver’s WDFQUEUE configuration The driver’s synchronization scope configuration The overall state of the WDFDEVICE and WDFQUEUE.

Framework Receives a Request and Inserts It on a WDFQUEUE o Read, write, device control, and internal device control requests are presented to KMDF drivers through WDFQUEUEs. n n If the device has previously been defined as a filter device object The framework determines the correct WDFQUEUE into which to place the arriving WDFREQUEST The framework then checks the dispatch type for the chosen WDFQUEUE At this point, both a WDFQUEUE and an I/O event processing callback (if the queue dispatch type is not Wdf. Io. Queue. Dispatch. Manual) have been chosen

Framework Invokes the Driver's I/O Event Processing Callback o Dispatch Type of the Queue n n n Wdf. Io. Queue. Dispatch. Parallel o a new request is inserted into a WDFQUEUE configured for parallel dispatching Wdf. Io. Queue. Dispatch. Sequential o WDFQUEUEs configured for sequential dispatching provide for only one WDFREQUEST from the queue to be in progress at a time. Wdf. Io. Queue. Dispatch. Manual. o Inserting a request into a WDFQUEUE configured for manual dispatching never results in the framework calling a type-specific or default I/O event processing callback

Queue State o The state of a queue is managed by KMDF drivers and the framework using the following flags: n n n Wdf. Io. Queue. Accept. Requests. When set, this flag indicates that the queue is accepting requests. Wdf. Io. Queue. Dispatch. Requests. When set, this flag indicates that the queue is dispatching requests. Wdf. Io. Queue. No. Requests. When set, this flag indicates that the queue is currently empty. Wdfio. Queue. Driver. No. Requests. When set, this flag indicates that all requests that have been delivered from this queue to the driver are complete (that is, the driver has no outstanding requests from this queue in progress). Wdf. Io. Queue. Pn. PHeld – When set, this flag indicates that the queue is not accepting requests due to a Plug and Play or power management event.

decision process used by the framework o o If the queue’s dispatch type is Wdf. Io. Queue. Dispatch. Manual If the queue’s dispatch type is either Wdf. Io. Queue. Dispatch. Parallel or Wdf. Io. Queue. Dispatch. Sequential If the queue’s serialization scope is not Wdf. Synchronization. Scope. None, a lock is identified. The framework examines the number of requests in progress from the queue

Driver's I/O Event Processing Callback Executes o o o Validating the Arriving Request Processing the Arriving Request Satisfying the Request within the I/O Event Processing Callback Initiating a Request on the Device Sending a Request to Another Driver for Processing Alternatively, a driver might need to create and send one or more requests to another driver n n n Synchronousl Asynchronously, Specifying a Completion Routine Asynchronously, Without Specifying a Completion Routine.

Driver's Interrupt Event Callback Executes o Running at device IRQL has several consequences for the driver: n n Other system processes are blocked when the interrupt event callback executes, due to its high IRQL. Therefore, the amount of processing performed in this routine should be kept to a minimum. Very few KMDF or system functions can be called from the driver’s interrupt event callback, again due to its high IRQL. For example, a driver cannot call Wdf. Request. Complete from its interrupt event callback, because this function must be called at IRQL DISPATCH_LEVEL or below.

Driver's Interrupt Event Callback Executes o The typical KMDF driver performs four basic actions in its interrupt event callback: n n Ensures that its device is interrupting. Saves the reason for the interrupt and/or data from the interrupt. Acknowledges the interrupt. f additional work is required, requests a callback to its Dpc. For. Isr

Summary o o This paper describes the major steps that an I/O request from user mode takes as it is processed by Windows, with the goal of helping you understand the overall flow of an I/O request through the system. This paper also describes the major steps that a KMDF driver takes to process a WDFREQUEST. These steps are defined by the device type, driver design, WDFQUEUE and dispatching configuration and the device’s synchronization scope.
