Windows Kernel Internals II Windows Driver Foundation University

  • Slides: 55
Download presentation
Windows Kernel Internals II Windows Driver Foundation University of Tokyo – July 2004 Dave

Windows Kernel Internals II Windows Driver Foundation University of Tokyo – July 2004 Dave Probert, Ph. D. Advanced Operating Systems Group Windows Core Operating Systems Division Microsoft Corporation © Microsoft Corporation 2004 1

Topics WDF Overview Toaster samples Framework objects (incl DEVQUEUES) Pn. P/Power Device Interface Generation

Topics WDF Overview Toaster samples Framework objects (incl DEVQUEUES) Pn. P/Power Device Interface Generation (DIG) © Microsoft Corporation 2004 2

Windows Driver Framework A library atop WDM: – Simpler interfaces – Handles most Pn.

Windows Driver Framework A library atop WDM: – Simpler interfaces – Handles most Pn. P and Power operations – Simplifies MP synchronization – Provides OO interfaces to drivers © Microsoft Corporation 2004 3

Requests Driver Pn. P/PM WMI I/O Target Framework Complete – Cancel – Forward Request

Requests Driver Pn. P/PM WMI I/O Target Framework Complete – Cancel – Forward Request Pipeline Legend Properties Events © Microsoft Corporation 2004 Methods IRP flow 4

Windows Driver Framework Architecture WDF defines: – Object properties – Object methods – Object

Windows Driver Framework Architecture WDF defines: – Object properties – Object methods – Object event callbacks (into drivers) – Object handles (used to reference objects) © Microsoft Corporation 2004 5

Simple Framework-based Driver A Driver. Entry routine which calls: – Wdf. Driver. Create An

Simple Framework-based Driver A Driver. Entry routine which calls: – Wdf. Driver. Create An Evt. Driver. Device. Add event callback: – called by Pn. P for hardware id match An Evt. Io. Start event callback – called when system has queued a request © Microsoft Corporation 2004 6

I/O Queue Event Callbacks Corresponds to IRP major codes for: – READ, WRITE, DEVICE_CONTROL,

I/O Queue Event Callbacks Corresponds to IRP major codes for: – READ, WRITE, DEVICE_CONTROL, INTERNAL_DEVICE_CONTROL and Cancellation Wdf. Fdo. Init. Set. Filter – Used to mark as driver as filter: • Any operation without callback registered bypasses driver © Microsoft Corporation 2004 7

Topics WDF Overview Toaster samples Framework objects (incl DEVQUEUES) Pn. P/Power Device Interface Generation

Topics WDF Overview Toaster samples Framework objects (incl DEVQUEUES) Pn. P/Power Device Interface Generation (DIG) © Microsoft Corporation 2004 8

Toaster Filter Sample - 1 Driver. Entry() – Initialize driver config to control the

Toaster Filter Sample - 1 Driver. Entry() – Initialize driver config to control the attributes that are global to the driver. – call Wdf. Driver. Create() – call Wdf. Collection. Create to create a collection object and store filter device objects. Filter. Evt. Driver. Unload() callback – call Wdf. Object. Dereference() to dereference collection © Microsoft Corporation 2004 9

Toaster Filter Sample - 2 Filter. Evt. Device. Add() callback – Evt. Device. Add()

Toaster Filter Sample - 2 Filter. Evt. Device. Add() callback – Evt. Device. Add() is called by the framework in response to Add. Device call from the Pn. P manager. – p. DO = Wdf. Fdo. Init. Wdm. Get. Physical. Device(). – use Io. Get. Device. Property() to decide to attach. – call Wdf. Fdo. Init. Set. Filter(). – call Wdf. Device. Create(). – call Wdf. Collection. Add() to add to collection (while holding the lock). – call our Filter. Create. Control. Device() routine. © Microsoft Corporation 2004 10

Toaster Filter Sample - 3 Filter. Create. Control. Device() routine – // create ctrl

Toaster Filter Sample - 3 Filter. Create. Control. Device() routine – // create ctrl DO so app can talk to filter directly. – call Wdf. Collection. Get. Count() to determine if exists – call Wdf. Control. Device. Init. Allocate() – call Wdf. Device. Init. Set. Exclusive(FALSE) – call Wdf. Device. Init. Update. Name(NAME_STRING) – call Wdf. Device. Create() – call Wdf. Device. Create. Symbolic. Link() – call Wdf. Device. Create. Default. Queue() to create device queue (callback is Filter. Evt. Device. Control. Ioctl) – call Wdf. Device. Finish. Initializing() to clear init flag in DO © Microsoft Corporation 2004 11

Toaster Filter Sample - 4 Filter. Evt. Device. Context. Cleanup() callback – acquire lock

Toaster Filter Sample - 4 Filter. Evt. Device. Context. Cleanup() callback – acquire lock (Wdf. Collection. Acquire. Lock) – call Wdf. Collection. Remove. Item() – n = Wdf. Collection. Get. Count() – if n==0 call Filter. Delete. Control. Device() routine – release lock (Wdf. Collection. Release. Lock) © Microsoft Corporation 2004 12

Toaster Filter Sample - 5 Filter. Delete. Control. Device() callback – call Wdf. Object.

Toaster Filter Sample - 5 Filter. Delete. Control. Device() callback – call Wdf. Object. Dereference(control. Device) Filter. Evt. Device. Control. Ioctl() callback – acquire lock (Wdf. Collection. Acquire. Lock) – n = Wdf. Collection. Get. Count() – call Wdf. Collection. Get. Item() n times – release lock (Wdf. Collection. Release. Lock) – call Wdf. Request. Complete. With. Information() © Microsoft Corporation 2004 13

Simple Toaster Function Sample Toaster. Evt. Device. Add() – – not sharing DO, so

Simple Toaster Function Sample Toaster. Evt. Device. Add() – – not sharing DO, so no collection needed – call Wdf. Device. Create. Device. Interface() – call Wdf. Device. Create. Default. Queue() to register IO callbacks, like Toaster. Evt. Io. Read() – do operation, then – call Wdf. Request. Complete. With. Information() © Microsoft Corporation 2004 14

Toaster Bus Sample - 1 Driver. Entry() – call Wdf. Driver. Create() w/ Bus_Evt.

Toaster Bus Sample - 1 Driver. Entry() – call Wdf. Driver. Create() w/ Bus_Evt. Device. Add() callback – Wdf. Device. Init. Set. Device. Type (FILE_DEVICE_BUS_EXTENDER) – call Wdf. Device. Init. Set. Exclusive(TRUE) – set callbacks: Bus_Evt. Device. List. Create. Pdo, Bus_Evt. Device. List. Id. Description{Duplicate, Compare, Cleanup} – Wdf. Fdo. Init. Set. Default. Device. List. Config() © Microsoft Corporation 2004 15

Toaster Bus Sample - 2 Bus_Evt. Device. Add() callback (cont) – call Wdf. Device.

Toaster Bus Sample - 2 Bus_Evt. Device. Add() callback (cont) – call Wdf. Device. Create() – call Wdf. Device. Create. Default. Queue() [ioctl] – call Wdf. Device. Create. Device. Interface() to create device interface – call Wdf. Fdo. Set. Bus. Information() – call our Bus_Wmi. Registration() and Bus_Do. Static. Enumeration() © Microsoft Corporation 2004 16

Toaster Bus Sample - 3 Bus_Evt. Device. Control() callback – call Wdf. Io. Queue.

Toaster Bus Sample - 3 Bus_Evt. Device. Control() callback – call Wdf. Io. Queue. Get. Device() – call Wdf. Request. Retrieve. Buffer() – switch on IOCTL PLUGIN_HARDWARE: Bus_Plug. In. Device() UNPLUG_HARDWARE: Bus_Un. Plug. Device() EJECT_HARDWARE: Bus_Eject. Device() – call Wdf. Request. Complete. With. Information() © Microsoft Corporation 2004 17

Toaster Bus Sample - 4 Bus_Plug. In. Device() [simulation] – init device description (descr)

Toaster Bus Sample - 4 Bus_Plug. In. Device() [simulation] – init device description (descr) – list = Wdf. Fdo. Get. Default. Device. List() – call Wdf. Device. List. Add. Or. Update. Child. Description. As. Present (list, descr) Bus_Un. Plug. Device() [simulation] – list = Wdf. Fdo. Get. Default. Device. List() – call Wdf. Device. List. Update. Child. Description-As. Missing (list, serialno) © Microsoft Corporation 2004 18

Toaster Bus Sample - 5 Bus_Eject. Device() [simulation] – list = Wdf. Fdo. Get.

Toaster Bus Sample - 5 Bus_Eject. Device() [simulation] – list = Wdf. Fdo. Get. Default. Device. List() – call Wdf. Device. List. Request. Child. Eject (list, serialno) Bus_Do. Static. Enumeration() [simulation] – read devices from registry to simulate boot enum – call Bus_Plug. In. Device() on each ‘device’ © Microsoft Corporation 2004 19

Toaster Bus Sample - 6 Bus_Evt. Device. List. Identification. Description{Duplicate, Compare, Cleanup}() callbacks –

Toaster Bus Sample - 6 Bus_Evt. Device. List. Identification. Description{Duplicate, Compare, Cleanup}() callbacks – duplicate a descriptor, compare 2 descriptors (by serialno), and free memory Bus_Evt. Device. List. Create. Pdo() callback – calls our Bus_Create. Pdo() routine © Microsoft Corporation 2004 20

Toaster Bus Sample - 7 Bus_Create. Pdo() routine – – Wdf. Device. Init. Set.

Toaster Bus Sample - 7 Bus_Create. Pdo() routine – – Wdf. Device. Init. Set. Device. Type(FILE_DEVICE_BUS_EXTENDER) Wdf. Device. Init. Set. Characteristics (…) Wdf. Device. Init. Set. Exclusive(FALSE) Wdf. Pdo. Init{Update. Device, Add. Hardware, Add. Compatible, Update. Instance}ID (Ids) to satisfy IRP_MN_QUERY_ID IRPs – call Wdf. Pdo. Init. Add. Device. Text() – call Wdf. Pdo. Init. Set. Default. Locale() © Microsoft Corporation 2004 21

Toaster Bus Sample - 8 – call Wdf. Pdo. Init. Set. Event. Callbacks() for

Toaster Bus Sample - 8 – call Wdf. Pdo. Init. Set. Event. Callbacks() for Bus_Pdo_Evt. Device. Resource. Requirements. Query – – call Wdf. Device. Create() init capabilities, call Wdf. Pdo. Set. Capabilities() call Wdf. Device. Add. Query. Interface() call Wdf. Device. Finish. Initializing() © Microsoft Corporation 2004 22

Toaster Bus Sample - 9 Bus_Pdo_Evt. Device. Resource. Requirements. Query() – call Wdf. Collection.

Toaster Bus Sample - 9 Bus_Pdo_Evt. Device. Resource. Requirements. Query() – call Wdf. Collection. Create(), Wdf. Resource. Io. Create(), and Wdf. Collection. Add() to collect resources – add our collection to the ‘collection of resource collections’ © Microsoft Corporation 2004 23

Topics WDF Overview Toaster samples Framework objects (incl DEVQUEUES) Pn. P/Power Device Interface Generation

Topics WDF Overview Toaster samples Framework objects (incl DEVQUEUES) Pn. P/Power Device Interface Generation (DIG) © Microsoft Corporation 2004 24

Framework Objects WDFDRIVER: a driver WDFDEVICE: a device WDFILEOBJECT: WDFMEMORY: WDFQUEUE: queue of I/O

Framework Objects WDFDRIVER: a driver WDFDEVICE: a device WDFILEOBJECT: WDFMEMORY: WDFQUEUE: queue of I/O requests WDFREQUEST: an I/O request WDFDPC: WDFTIMER: WDFWORKITEM: WDFINTERRUPT: © Microsoft Corporation 2004 25

Framework Object Collections Used to represent: – resource requirement lists – resource lists –

Framework Object Collections Used to represent: – resource requirement lists – resource lists – set of connected child devices – set of exported device interfaces – any set of framework objects in driver – collections of collections © Microsoft Corporation 2004 26

WDFQUEUE Object Supports numerous operations Requests enqueuing and dequeuing Controls concurrency of requests presented

WDFQUEUE Object Supports numerous operations Requests enqueuing and dequeuing Controls concurrency of requests presented to the driver Allows processing to pause and resume Requests cancellation and cancel-safe queues Synchronizes I/O operations with Pn. P/Power state transitions Reports outstanding I/O operations to Pn. P/Power stage Serializes event callbacks Defers event callbacks to comply with PASSIVE_LEVEL constraints © Microsoft Corporation 2004 27

WDFQUEUE Request Events WDFQUEUE objects use callbacks to notify driver of WDFREQUEST events –

WDFQUEUE Request Events WDFQUEUE objects use callbacks to notify driver of WDFREQUEST events – Evt. Io. Read – IRP_MJ_READ requests – Evt. Io. Write – IRP_MJ_WRITE requests – Evt. Io. Device. Control – device control requests – Evt. Io. Cancel – a request is cancelled – Evt. Io. Stop – a power state change requested – Evt. Io. Start – request w/o a specific callback © Microsoft Corporation 2004 28

WDFQUEUE Concurrency “in-flight” requests: – received from queue, not yet completed Concurrency control for

WDFQUEUE Concurrency “in-flight” requests: – received from queue, not yet completed Concurrency control for “in-flight” requests – Serial, single request model – Parallel model – Manual model WDF may ask cancel/suspend “in-flight” requests – due to IO cancel, Pn. P/Power events, dev removal – driver implements Evt. Io. Cancel/Evt. Io. Stop callbacks Auto cancel/suspend of queued requests © Microsoft Corporation 2004 29

WDFQUEUE Power Management Power management of WDFQUEUEs – Enabled by default – Configurable on

WDFQUEUE Power Management Power management of WDFQUEUEs – Enabled by default – Configurable on a per WDFQUEUE basis Advantages of power-managed queues – Notify Pn. P/Power stage of arriving I/O requests so that device power can be restored – Notify Pn. P/Power stage of empty queue so that device can be powered down – Notify driver of power-state changes for in-flight requests through the Evt. Io. Stop callback © Microsoft Corporation 2004 30

WDFQUEUE Serialization and Constraints Outstanding I/O request serialization – I/O requests received from a

WDFQUEUE Serialization and Constraints Outstanding I/O request serialization – I/O requests received from a WDFQUEUE are asynchronous – Requests completed in event callback or later – Driver configures number of concurrent I/O operations per queue Constraints on concurrent execution of event callbacks – Set in WDF_OBJECT_CONSTRAINTS – Control simultaneous event callbacks (not actual I/O operations) – Help manage shared access to WDFQUEUE context memory Callbacks can have PASSIVE_LEVEL constraint – WDFQUEUE automatically invokes the callback from a system work item if required © Microsoft Corporation 2004 31

Object Context Memory Can be associated with any WDF object Similar to a device

Object Context Memory Can be associated with any WDF object Similar to a device extension Provides storage for a drivers object-specific information Allocated from non-paged pool in driver-supplied size and type Macros assist in defining the type from a C struct Accessed through pointer stored/retrieved through the object handle Object’s can have more than one memory context, if the types differ Optional event callback Evt. Object. Destroy deallocates context when the object handle is destroyed © Microsoft Corporation 2004 32

Asynchronous Processing Objects used for asynchronous events – WDFDPC, WDFTIMER, WDFWORKITEM Associated with a

Asynchronous Processing Objects used for asynchronous events – WDFDPC, WDFTIMER, WDFWORKITEM Associated with a WDFDEVICE or WDFQUEUE Automatically handle race conditions Asynchronous processing can serialize with an object’s event callbacks IRQL of the object must be compatible © Microsoft Corporation 2004 33

WDFINTERRUPT / WDFDPC Supports – Wire line and message signaled interrupts – Notification of

WDFINTERRUPT / WDFDPC Supports – Wire line and message signaled interrupts – Notification of assignment of interrupt resources – DIRQL synchronization functions – Associated with WDFDEVICE object Evt. Interrupt. Isr callback – services interrupt, stores in context memory – after dismissing, calls Wdf. Interrupt. Queue. Dpc. For. Isr © Microsoft Corporation 2004 34

I/O Targets Target forwarding request – local I/O target: next driver in stack –

I/O Targets Target forwarding request – local I/O target: next driver in stack – remote I/O target: some other driver – I/O targets list where requests went (for cancel) – can be general or specialized (e. g. USB) I/O target states: – Started, Query-stop, Stopped, Query-remove, Removed, Surprise-removed, Closed © Microsoft Corporation 2004 35

Topics WDF Overview Toaster samples Framework objects (incl DEVQUEUES) Pn. P/Power Device Interface Generation

Topics WDF Overview Toaster samples Framework objects (incl DEVQUEUES) Pn. P/Power Device Interface Generation (DIG) © Microsoft Corporation 2004 36

WDF Pn. P/Power Design Goals Remove as much boilerplate as possible Driver callbacks only

WDF Pn. P/Power Design Goals Remove as much boilerplate as possible Driver callbacks only for “interesting” events Automatically provide good default Pn. P behavior – Rebalance, Removal, Surprise Removal Automatically provide good default Power behavior – Support Sleep/Hibernate, “Fast Resume”, idle-time power management Provide clear error-handling paths – Some software errors automatically handled – Some hardware errors handled by resetting device © Microsoft Corporation 2004 37

WDF Pn. P/Power Design Goals Integrate driver primitives with Pn. P/Power actions Automatically stop

WDF Pn. P/Power Design Goals Integrate driver primitives with Pn. P/Power actions Automatically stop presenting requests when leaving D 0 (high-power) When leaving D 0 disconnect interrupts, Stop DMA & I/O Targets All Pn. P/Power callbacks at PASSIVE Remove need for drivers to track state Callback primitives small w/ straightline code © Microsoft Corporation 2004 38

WDF Bus Drivers Trivial to write WDF can handle most of the details: –

WDF Bus Drivers Trivial to write WDF can handle most of the details: – Reporting children to WDM – Coordinating scanning for children – Maintaining the list of children Drivers responsible for: – Identifying children – Generating IDs – Generating resource requirements – Identifying capabilities – Notification that children have been removed © Microsoft Corporation 2004 39

Power Managed Queues (optionally) aware of device power state Device hardware held in high-power

Power Managed Queues (optionally) aware of device power state Device hardware held in high-power state until requests completed or marked as “stopped” Requests queued and not presented to driver until machine fully resumes from a sleep state and device is in D 0 Not all queues are power-managed: – Queues for requests that touch hardware should be power-managed – Device Control queues and queues in software-only drivers usually should NOT be power-managed © Microsoft Corporation 2004 40

Power Policy Ownership WDF provides a rich set of automatic behaviors Device to low-power

Power Policy Ownership WDF provides a rich set of automatic behaviors Device to low-power when the system goes to sleep/hibernate Device to low-power when the device is idle Device to high-power when there are requests to process Automatic arming for wake while the system is running (device is idle) Automatic arming for wake while the system is sleeping © Microsoft Corporation 2004 41

Simplest WDF Driver Only required Pn. P/PM fcn: Evt. Device. Add 1. Set some

Simplest WDF Driver Only required Pn. P/PM fcn: Evt. Device. Add 1. Set some device constraints 2. Create a WDFDEVICE object 3. Create queues for handling requests WDF handles Pn. P/PM events automatically [If Evt. Device. Add allocates state, must provide Evt. Device. Context. Cleanup] © Microsoft Corporation 2004 42

Simple Pn. P/PM Callback Groups For all devices with hardware • Evt. Device. D

Simple Pn. P/PM Callback Groups For all devices with hardware • Evt. Device. D 0 Entry – everytime device turned-on • Evt. Device. D 0 Exit – everytime device turned-off For all devices which use interrupts • Evt. Interrupt. Enable – called after Evt. Device. D 0 Entry • Evt. Interrupt. Disable – Called before Evt. Device. D 0 Exit For all devices which have memory-mapped registers • Evt. Device. Prepare. Hardware – one-time setup ops • Evt. Device. Release. Hardware For all USB devices • Evt. Device. Prepare. Hardware © Microsoft Corporation 2004 43

Advanced Power Management Drivers opt into advanced PM – Devices only in D 0

Advanced Power Management Drivers opt into advanced PM – Devices only in D 0 when there is work – Otherwise devices in a low-power state – Devices ->D 0 by power-managed queues – Devices ->D 0 when wake signals trigger Very little code needed. Driver provides: – Arm/Disarm wake callbacks – Info on idle detection and D-states for idle © Microsoft Corporation 2004 44

Pn. P Child Enumeration Properties for static data – Bus instance ID – Compatible

Pn. P Child Enumeration Properties for static data – Bus instance ID – Compatible IDs – Hardware IDs, etc. Callbacks for dynamic data and child specific actions – Associated resources – Eject – Create child Two conceptual API groupings – “Software” child device APIs – “Hardware” child device APIs © Microsoft Corporation 2004 45

Software (“static”) Children Enumerated as result of – Request from user mode – Registry

Software (“static”) Children Enumerated as result of – Request from user mode – Registry setting – Hard coded logic in the driver Once enumerated, rarely reported missing Simple API for reporting child to WDF © Microsoft Corporation 2004 46

Hardware (“dynamic”) Children These devices come/go frequently True physical dependents of the parent Enumeration

Hardware (“dynamic”) Children These devices come/go frequently True physical dependents of the parent Enumeration driven by bus events Redetection of child when parent ->D 0 WDFDEVICELIST simplifies enumeration – Parent reports arrival/departure of child – Reporting asynchronous with scanning © Microsoft Corporation 2004 47

WDFDEVICELIST APIs Scanning – Wdf. Device. List{Begin, End}Scan Updating status – Wdf. Device. List{

WDFDEVICELIST APIs Scanning – Wdf. Device. List{Begin, End}Scan Updating status – Wdf. Device. List{ Add. Or. Update. Child. Description. As. Present, Update. Child. Description. As. Missing, Request. Child. Eject} List Iteration – Wdf. Device. List{Begin, End}Iteration – Wdf. Device. List. Get. Next. Device © Microsoft Corporation 2004 48

Child Device Identification WDFDEVICELIST uniquely ids children Two types of identification: – identification description:

Child Device Identification WDFDEVICELIST uniquely ids children Two types of identification: – identification description: how device is found on bus (fixed) – address description: how device is spoken to on bus (dynamic) © Microsoft Corporation 2004 49

WDFDEVICELIST Callbacks Only the bus driver knows the following – How big the ID

WDFDEVICELIST Callbacks Only the bus driver knows the following – How big the ID description is – If an address description is required – How to compare two ID descriptions – How to copy an ID – How to cleanup an ID’s buffer © Microsoft Corporation 2004 50

Topics WDF Overview Toaster samples Framework objects (incl DEVQUEUES) Pn. P/Power Device Interface Generation

Topics WDF Overview Toaster samples Framework objects (incl DEVQUEUES) Pn. P/Power Device Interface Generation (DIG) © Microsoft Corporation 2004 51

Device Interface Generation Replace IOCTL as programming model with something more client/server-like Goals –

Device Interface Generation Replace IOCTL as programming model with something more client/server-like Goals – type-safety – simplified driver code – separate interfaces and implementation – enable new transports (i. e. not just syscall) © Microsoft Corporation 2004 52

What is a Device Interface A contract between client and driver that defines: –

What is a Device Interface A contract between client and driver that defines: – Operations, parameters, results, and constraints – Access permission required, IRQ Level, etc Interface Definition should drive the implementation – Define the interface as “how the implementation works” Interface Definition does not explicitly address: – Transfer mode, transport mechanism, packet format – Separate interface from its binding to a particular transport © Microsoft Corporation 2004 53

DIG plan Basic strategy – Extract interfaces from the code – Specify interfaces abstractly

DIG plan Basic strategy – Extract interfaces from the code – Specify interfaces abstractly in XML – “Regenerate” the interface code from XML Advantages/opportunities – DIG can generate interface code for multiple languages – Provide help for static verification – Use multiple IOCTL codes underneath to improve efficiency – Generate wrappers (stubs) for clients – Impedance-match 32 -bit clients to 64 -bit drivers © Microsoft Corporation 2004 54

Discussion © Microsoft Corporation 2004 55

Discussion © Microsoft Corporation 2004 55