io request flow in wdf kernel-mode drivers 電機所通訊與網路組 694415036 胡育誌

30
IO Request Flow in WDF Kernel-Mode Drivers 電電電電電電電電電 694415036 電電電

Post on 20-Dec-2015

243 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

IO Request Flow in WDFKernel-Mode Drivers

電機所通訊與網路組694415036

胡育誌

Page 2: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

Introduction

• Every Windows driver must handle I/O requests,even if all the driver does is forward them to another driver

• Two part:– Part one: overall path of an I/O request from t

he initial request by application– Part two: KMDF driver processes an I/O reque

st

Page 3: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

Part 1

• Application I/O Requests

• Common I/O Requests– Create– Close– Read & Write– Device Control

• Synchronous and Asynchronous Application Request

Page 4: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

I/O Request Path from Application to Driver

USBHUB

System Service Dispatcher

I/O Manager

Kernel Mode

User Mode

1

Applicationstatus = ReadFile(handle,...)

USBPORT

PCI

UsbDev.sys(KMDF driver)

KMDF Library(Wdfdynam.sys)

3

5 4

2

1

6

I/O Request Path

Page 5: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

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

Page 6: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

Validating request parameters

• Always checks to ensure that the handle used is valid.

• Always checks that the user has appropriate access to the device (that is, the user has read access if a read request is being issued, or write access for a write request).

• If the request uses buffers, checks to ensure that the user-supplied data buffer pointers and data buffer lengths are valid.

Page 7: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

Building the IRP

• Allocates an IRP of the appropriate size (that is, one with enough I/O stack locations for all drivers in the driver stack).

• Initializes the fixed-size part of the IRP.• Initializes the first I/O stack location in the I/O stack. This

first I/O stack location contains the request parameters that will be used by the first driver to receive the I/O request. Starting with the first driver, each driver in the stack is responsible for initializing the I/O stack location for the next-lower driver before forwarding the IRP to that driver.

• The IRP major function code.• A description of the user’s data buffer

Page 8: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

Sending the IRP to the framework

• IoCallDriver

Page 9: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

I/O Request Path from Framework to Driver

Driver Callbacks

IRP_MJ_PNPIRP_MJ_POWER

IRP_MJ_SYSTEM_CONTROL (KMDF only)

I/O Requests(IRP_MJ_READIRP_MJ_WRITEIRP_MJ_DEVICE_CONTROL)

IRP_MJ_Xxx

Driver Callbacks

Driver Callbacks

Dispatcher

I/O Package

Plug and Play/Power

Package

WMI Package

I/O Target

Non-power-managed I/O

Queues

...

Power-managed I/O Queues

Page 10: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

The Framework Routes the Request to the Appropriate Package

The I/O Package Checks the I/O Request Type– Create (IRP_MJ_CREATE)– Cleanup (IRP_MJ_CLEANUP)– Close (IRP_MJ_CLOSE)– Read (IRP_MJ_READ)– Write (IRP_MJ_WRITE)– Device control (IRP_MJ_DEVICE_CONTROL)– Internal device control (IRP_MJ_INTERNAL_DEVICE

_CONTROL)

Page 11: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

The I/O Package Further Validates Request Parameters

• Read and write requests

• Device control requests

Page 12: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

The Framework Processes the Request

• Handles the request on behalf of the driver

• Forwards the requests to the driver for the next-lower device in the device stack

• Creates a WDFREQUEST object that represents the arriving request and passes it to the KMDF driver

Page 13: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

KMDF Driver Processing of I/O Requests

• Processing a Request Internally

• Processing a Request by Sending It to Another Driver

• Initializing the next I/O stack location in the request – WdfRequestCopyCurrentStackLocationToNex

t– WdfIoTargetFormatRequestForXxxx

Page 14: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

• Creating and sending new requests

• Sending requests synchronously or asynchronously

• Establishing an I/O completion routine callback

Page 15: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

Requests Marked Pending During Processing

• If the application has requested asynchronous processing for this I/O request, control is returned to the calling thread.

• If the application has requested synchronous processing, the I/O Manager places the thread in a wait state until the request is ultimately completed.

Page 16: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

I/O Completion Path from Driver to Application

USBHUB

System Service Dispatcher

I/O Manager

Kernel Mode

User Mode

Applicationstatus = ReadFile(handle,...)

USBPORT

PCI

UsbDev.sys(KMDF driver)

KMDF Library(Wdfdynam.sys)

3

5 4

2

1

6

E

D

C

B

A

I/O Completion Path

I/O Request Path

I/O statusCompletion informationBuffered data

Page 17: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

Driver Completion of a Request

• I/O status

• Completion information

• Requested data

Page 18: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

I/O Completion Path from Framework to I/O Manager

• I/O Manager Completion of a Request– Stage 1: Completion processing that might tak

e 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.

Page 19: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

Part 2: Flow of an I/O Request Within a KMDF Driver

• Factors that influence the path of an I/O request within a KMDF driver– The type of device being supported– The driver’s WDFQUEUE configuration – The driver’s synchronization scope configurati

on– The overall state of the WDFDEVICE and WD

FQUEUE

Page 20: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

Framework Receives a Request and Inserts It on a WDFQUEUE

• WDFQUEUE Insertion Logic

• Driver Queue Implementation

Page 21: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

Framework Invokes the Driver's I/O Event Processing Callback

• Dispatch Type of the Queue– Dispatch Types

• WdfIoQueueDispatchParallel• WdfIoQueueDispatchSequential• WdfIoQueueDispatchManual

• Impact of Dispatch Type on Driver Design

Page 22: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

Synchronization Scope for WDFDEVICEs and WDFQUEUEs

• Specifying Synchronization Scope

• How Synchronization Scope Works

Page 23: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

Queue State

• WdfIoQueueAcceptRequests

• WdfIoQueueDispatchRequests

• WdfIoQueueNoRequests

• WdfioQueueDriverNoRequests

• WdfIoQueuePnPHeld

Page 24: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

Driver's I/O Event Processing Callback Executes

• 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

Page 25: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

• I/O Request Completion Examples

• WDFDIO Synchronous Request Completion

• WDFDIO Initiating a Request and Awaiting an Interrupt

• OSRUSBFX2 Sending a Request and Awaiting Completion

Page 26: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

• OSRUSBFX2 Sending an Alternate Request Synchronously

Page 27: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

Driver's I/O Completion Routine Executes

VOID• EvtRequestReadCompletionRoutine(• IN WDFREQUEST Request,• IN WDFIOTARGET Target,• PWDF_REQUEST_COMPLETION_PARAMS CompletionParams,• IN WDFCONTEXT Context• )• { • NTSTATUS status;• size_t bytesRead = 0;• PWDF_USB_REQUEST_COMPLETION_PARAMS usbCompletionParams;• UNREFERENCED_PARAMETER(Target);• UNREFERENCED_PARAMETER(Context);• status = CompletionParams->IoStatus.Status;• • usbCompletionParams = • CompletionParams->Parameters.Usb.Completion;• • bytesRead = usbCompletionParams->Parameters.PipeRead.Length;• • WdfRequestCompleteWithInformation(Request, status, bytesRead);• return;• }

Page 28: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

Driver's Interrupt Event Callback Executes

• Interrupt Event Callback Example

Page 29: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

Driver's DpcForIsr Callback Executes

• DpcForIsr Callback Example

Page 30: IO Request Flow in WDF Kernel-Mode Drivers 電機所通訊與網路組 694415036 胡育誌

Summary

• understand the overall flow of an I/O request through the system. It also shows where the kernel-mode driver framework (KMDF) fits within the process, and the type of work that the framework performs

• are defined by the device type, driver design, WDFQUEUE and dispatching configuration and the device’s synchronization scope. Because of the various processing and synchronization options, a well-designed KMDF driver will typically require few, if any, manually acquired locks to properly serialize access to its shared data structures.