Содержание
- 2. Outline Architectural Goals Architectural Description Core components Driver Manager, Reflector, Host Process, and Drivers Driver startup
- 3. Goals An understanding of UMDF infrastructure An understanding of the UMDF DDIs and how they are
- 4. Architectural Goals Stability Driver failures should NOT bring down the system Build on Windows NT I/O
- 5. Device Stack Device Stack Architectural Block Diagram Windows Kernel (I/O Mgr, PnP) Reflector Driver Manager Kernel
- 6. UMDF Components Driver manager Global, system-wide Windows Service Responsible for host process lifetime Responds to messages
- 7. UMDF Components Reflector Nucleus of UMDF Installed with each device stack Proxy for UM device stack
- 8. UMDF Components : Host Process Child process of the UM Driver Manager Unit of isolation for
- 9. Host Process - Framework Implements all of the WDF model Queues, Request, I/O target, etc Implements
- 10. IPC : Message Passing Requirements Packet-based and asynchronous Cancelable Fail-safe Efficient data transfer Secure Several potential
- 11. Processing Messages KM drivers handle I/O in caller’s thread Obviously not a option for UMDF Host
- 12. Driver Loading Windows Kernel (I/O Mgr, PnP) Driver Manager Reflector Kernel Driver 1 3 Add Device
- 13. Driver Loading (Layered UM Drivers) Add Device
- 14. Driver Loading (Host Process) UM Driver Host Runtime Framework
- 15. Host Process I/O Data Flow Kernel driver Windows Kernel “Up” Device Object Application UM Filter Driver
- 16. I/O in Host Process UM Driver Run-time Framework Device Stack Invoke driver callback passing in I/O
- 17. Device Removal and Cleanup UM Driver Run-time Framework Device Remove Message path similar to “add device”
- 18. When Driver or Application Crashes When the UM driver crashes: Reflector gets notification from OS Reflector
- 19. Timeout Policy UMDF enforces timeouts on “critical” operations Operations that run under system wide locks Operations
- 20. Impersonation Driver runs in LocalService security context Drivers can impersonate the client process Only for I/O
- 21. Using Impersonation Application controls the allowed level Specified in QOS settings in CreateFile API See dwFlagsAndAttributes
- 22. UMDF Verifier Built-in verification Checks for problems in framework code Checks for problems in device driver
- 23. UMDF Verifier Failures Driver failures will “Bugcheck” the host Bugcheck is NOT the “Blue Screen of
- 24. UMDF Error Reporting Current driver failure reports are hard to analyze Too much information in the
- 25. Types of Error Reports UMDF will report the following problems : UM Driver Verifier failure Unhandled
- 26. Device Driver Interfaces (DDI) and Programming Patterns
- 27. Framework objects have a hierarchical relationship WDF Driver is the root object Child lifetime scoped by
- 28. Key Framework Objects
- 29. Parent-Child Object Hierarchy Device I/O Queue I/O Request I/O Target Driver File Applies to lifetime management
- 30. Framework Object Model MyDriver MyRead Queue MyWrite Queue Framework Driver Driver implemented Callback Objects WDF Objects
- 31. Framework Object Model MyDriver1 MyDevice1 MyRead Queue1 MyWrite Queue1 Framework Driver 1 WDF Driver WDF Device
- 32. DDI Design Common object model with KMDF Driver writers familiar with KMDF can quickly come up
- 33. DDI : COM Interfaces Problems solved by Interfaces Allows us to evolve the DDI without changing
- 34. All You Need to Know about COM … COM interfaces, by convention, start with “I” e.g.,
- 35. Device Driver Interfaces (DDI) UMDF DDI is in wudfddi.idl interface IWDFObject : IUnknown { HRESULT DeleteWdfObject();
- 36. Device Driver Interfaces (DDI) DDIs are grouped into 2 types of interfaces Functionality exported by Framework
- 37. Device Driver Interfaces (DDI) KMDF and UMDF DDI are similar Tuned to language and runtime environment
- 38. Driver Entry IDriverEntry is the top driver-exported interface interface IDriverEntry::IUnknown { HRESULT OnInitialize( IWDFDriver* pWdfDriver );
- 39. Callback objects = Callbacks + Context Example: Creating Device Object HRESULT CMyDriver::OnDeviceAdd( IWDFDriver* pDriver, IWDFDeviceInitialize* pDeviceInit
- 40. Callback Objects (con’t) class CMyDevice : public IDevicePnpHardware // Callback interface exposed to // framework. {
- 41. Callback Objects (con’t) static HRESULT CreateInstance( IUnknown **ppUnknown, IWDFDeviceInitialize *pDeviceInit, HANDLE CompletionPort ) { ... //
- 42. Callback Objects (con’t) WDF Driver Framework Driver IDriverEntry 5
- 43. Summary Discussed driver loading/unloading, I/O data flow Driver installation and setup are same as WDM drivers
- 44. How to Develop a UMDF Driver Part 2
- 45. Outline Goals Getting Started Writing your Driver Installing your Driver Debugging the Driver Plug and Play
- 46. Goals How to start writing and debugging a UMDF driver How to interact with UMDF’s Plug
- 47. Writing Your Driver
- 48. What is a User-Mode Driver? User-Mode (UM) Drivers are DLLs Provide driver’s “callback object” classes Use
- 49. UMDF Driver DLL Exports DllMain Called when DLL loads (and unloads) Construct global objects, initialize tracing
- 50. Common Driver Classes COM objects must implement IUnknown Independently, or with help from CUnknown base class
- 51. UMDF Skeleton Driver Example code based on UMDF Skeleton sample Code in slides should not be
- 52. Example: CMyDriver Definition (Driver.h) class CMyDriver: public CUnknown, public IDriverEntry { IDriverEntry *QueryIDriverEntry(); HRESULT Initialize(); public:
- 53. Example: CMyDriver Implementation (Driver.cpp) HRESULT CMyDriver::CreateInstance( CMyDriver **Driver ) { CMyDriver *driver = new CMyDriver(); if
- 54. Example: CMyDriver Implementation (con’t) HRESULT CMyDriver::OnDeviceAdd(...) { CMyDevice *deviceCallback; IWDFDevice *fxDevice; HRESULT hr = CMyDevice::CreateInstance( FxDeviceInit,
- 55. Example: CMyDevice Definition (Device.h) class CMyDevice : public CUnknown { HRESULT Initialize( IWDFDeviceInitialize *DeviceInit ) {
- 56. Writing Your Driver (Summary) Create the common code Required DLL Exports COM support classes Copy from
- 57. Installing Your Driver INF Based Installation Just like WDM and kernel-mode WDF drivers Device Matching, Driver
- 58. Registry Locations UMDF keeps information in several registry keys We’ll refer to them using these names
- 59. System provided CoInstaller Sets Driver Manager to start automatically WUDFCoInstaller.dll is already installed No need to
- 60. UMDF uses CoCreateInstance to load drivers This requires the driver to be “registered” INF must register
- 61. UMDF loads drivers by “Driver Name” We recommend the binary name (without extension) UMDF Maintains its
- 62. Setup UM Driver Key Create an entry under UMDF Services Key One for each driver installed
- 63. Reflector driver is WUDFRd.sys Already installed on the system Your INF must make this the top-most
- 64. Installing Your Driver (Summary) Use the UMDF CoInstaller Register your Driver DLLs with COM Configure the
- 65. Debugging Your Driver
- 66. Debugging Overview Similar to service debugging UM Drivers cannot be started by the debugger No Just-In-Time
- 67. Debugger Options WinDbg GUI Debugger Source and/or Assembly level debugging UMDF debugger extension available Can work
- 68. User-Mode or Kernel-Mode Debugging Can debug UMDF with UM or KM debugger You can use WinDbg
- 69. Host process started by Driver Manager service You can’t attach debugger until after initialization Host Process
- 70. Cannot connect UM Debugger until host starts Enable the Initial Breakpoint Attach the device to the
- 71. UMDF Debugger Extension WudfExt.dll Copy into your path “!load WudfExt.dll” debugger command loads the extension Names
- 72. Interesting UMDF Breakpoints (BP) Hopefully, this will get you started
- 73. Tracing in Your UM Driver Recommend using WPP tracing for debug output Lightweight & always available,
- 74. What Do I Do When It Crashes? (Summary) Check for configuration problems Is there a PNP
- 75. Plug and Play / Power Management
- 76. UMDF Design Goals for PnP/PM Coordinate I/O delivery to driver with PnP/PM events The framework handles
- 78. UMDF PnP/PM Design Implemented with a state machine Consumes PnP/Power messages from the reflector Suspends and
- 79. UMDF PnP/PM Interfaces IDevicePnpHardware First time hardware initialization / de-initialization OnPrepareHardware() OnReleaseHardware() IDevicePnp Device state change
- 80. Device Driver Interface (DDI) IDriverEntry::OnDeviceAdd Driver must implement this interface Called when framework receives “Add Device”
- 81. IDevicePnpHardware OnPrepareHardware Called when device is first started Driver establishes connection to the device and performs
- 82. IDevicePnpHardware OnReleaseHardware Called when device is removed or stopped Driver should essentially undo anything it did
- 83. IDevicePnp OnD0Exit Called each time device should power down Also called before the device is removed
- 84. IDevicePnp OnSurpriseRemoval Called when the device has unexpectedly detached from the PC Stop device’s I/O target
- 85. IDevicePnpSelfManagedIo Notifications to drivers that perform I/O dispatching unmanaged by the framework OnSelfManagedIoInit() Start I/O dispatching
- 86. PnP/PM Driver Callback Example
- 87. Device Driver Off On Off Framework Start Device OnPrepareHardware()
- 88. Device Driver Off On Off Framework OnD0Entry() Start Device
- 89. Device Driver Off On On Device Operational Framework
- 90. Device Driver Off On On OnD0Exit() Suspend Device Framework
- 91. Device Driver Off On On OnD0Entry() Resume Device Framework
- 92. Device Driver Off On On OnD0Exit() Remove Device Framework
- 93. Device Driver Off On On OnReleaseHardware() Remove Device Framework
- 94. WDM Message to UMDF Callback Mapping Start -> Query Remove -> Remove IRP_MN_START_DEVICE: OnPrepareHardware() OnD0Entry() OnSelfManagedIoInit()
- 95. WDM Message to UMDF Callback Mapping Start -> Surprise Removal -> Remove IRP_MN_START_DEVICE: OnPrepareHardware() OnD0Entry() OnSelfManagedIoInit()
- 96. PnP/PM Timeouts Each WDM PnP/PM message has a timeout Currently, this timeout is set to 1
- 97. Supplemental Slides
- 98. Example: DllMain BOOL DllMain( HINSTANCE ModuleHandle, DWORD Reason, PVOID Reserved ) { if (DLL_PROCESS_ATTACH == Reason)
- 99. Example: DllGetClassObject HRESULT DllGetClassObject( REFCLSID ClassId, REFIID InterfaceId, PVOID *Interface ) { PCClassFactory factory; HRESULT hr;
- 100. Example: DllRegisterServer & DllCanUnloadNow HRESULT DllRegisterServer() { return UpdateCOMRegistration( g_ModuleHandle, IDR_MYDRIVER_CLASSINFO, true, __uuidof( MyDriverCoClass ), L“UMDF
- 101. Example: CUnknown Definition class CUnknown : public IUnknown { LONG m_RefCount; public: CUnknown() { m_RefCount =
- 102. Example: CUnknown Implementation ULONG CUnknown::AddRef(){ return InterlockedIncrement(&m_RefCount); } ULONG CUnknown::Release() { ULONG count = InterlockedIncrement(&m_RefCount); if
- 103. Example: CClassFactory Definition class CClassFactory : public CUnknown, public IClassFactory { public: IClassFactory *QueryIClassFactory() {...} //
- 104. Example: CClassFactory Implementation HRESULT CClassFactory::QueryInterface(...) { if(IsEqualIID(Interface, __uuidof(IClassFactory)) { *Object = QueryIClassFactory(); return S_OK; } else
- 105. Debugging with WinDbg Source Level GUI debugger (Most) commands work on addresses or symbols ModuleName!FunctionName ModuleName!ClassName::MethodName
- 106. Debugging with a Kernel Debugger WinDbg can be used as a kernel debugger Must point Kernel
- 107. Collecting & Viewing Traces TraceLog.exe lets you control trace sessions To start a session Tracelog.exe -start
- 108. How to Develop a UMDF Driver: Part 3
- 109. Outline I/O Processing Overview I/O Queues File Objects Driver Layering I/O Targets Cancellation
- 110. Goals A better understanding of: How to incorporate I/O processing in a UMDF driver Framework objects
- 111. I/O Request Processing Overview
- 112. I/O Request Processing in UMDF You’ve seen WDF Driver and Device objects These are used to
- 113. Request Framework Objects Device Queue I/O Target Memory (Input) Memory (Output) Driver File
- 114. Framework Objects These objects are associated with request processing All derive from IWDFObject
- 115. I/O Request Processing – A Bird’s Eye view Request I/O Queue Driver I/O Target Complete
- 116. I/O Request Object I/O Queue Driver I/O Target Complete Request
- 117. I/O Request Object Represents a request Sent to the driver by a client An application or
- 118. I/O Queues
- 119. I/O Queues I/O Queue Driver I/O Target Complete Request
- 120. I/O Queues Queues provide flow control for I/O Requests All requests dispatched to driver through I/O
- 121. I/O Queue Dispatch Types Dispatch Type controls how requests are presented to driver Queues allow following
- 122. Automatic Dispatch Queue presents request through driver implemented callback interfaces IQueueCallbackCreateClose IQueueCallback[Read | Write | DeviceIoControl]
- 123. Manual Dispatch Driver pulls requests from the queue Typically starts in response to Queue callback IQueueCallbackStateChange
- 124. Callback Constraints Callback constraints specify locking model for Queue callbacks Callback constraints options Device Level: Callbacks
- 125. Callback Constraints (con’t) Constraints apply only to Queue Callbacks Queue callbacks include Automatic Dispatch Callbacks Queue
- 126. Dispatch Type vs. Locking Model Request1 Req1 Complete Request2 Req2 Complete Request1 Req1 Complete Request2 Req2
- 127. UMDF OSR USB Sample Walk-through Requests and Queue Usage in the sample driver (src\umdf\usb\driver) Callback locking
- 128. File Objects
- 129. File Object File Object represents open connection to device It is the session context for I/O
- 130. File Object (con’t) Device File1 Request1 Request2 File2 Request1 Request2
- 131. File Object Lifetime File Object’s lifetime spans Create: File Object gets created in response to a
- 132. File Object Lifetime Illustrated Close … Any remaining Requests drain … Cleanup CloseHandle(hFile); … … Read/Write/IoCtl
- 133. Cleanup Handling in Driver Cleanup received when client closes connection Notification for driver to flush I/O
- 134. UMDF OSR USB Sample Walk-through File usage in the sample driver (src\umdf\usb\driver) (Queue.cpp) CMyQueue::OnRead CMyQueue::OnCreateFile CMyQueue::OnCleanupFile
- 135. Driver Layering
- 136. Driver Layering UMDF supports driver layering Multiple drivers build a device stack “Function” driver provides core
- 137. Driver Layering Diagram Device Queue File Request Device Queue File Request Device Queue File Request Device
- 138. Request Completion When Driver receives a Request it can Complete the Request synchronously Forward it to
- 139. Request Completion Illustrated Request Completion Routine Request Completion Routine Request
- 140. I/O Target
- 141. I/O Target I/O Queue Driver I/O Target Complete Request
- 142. I/O Target Framework Object Provides a way to send requests to another device Local I/O Target
- 143. Device Stack I/O Target I/O Target I/O Target Device Device Device Local I/O Target User Mode
- 144. I/O Target Functionality State based I/O handling Driver can start and stop the I/O target In
- 145. Bridging UM and KM stacks I/O Target provides bridge between KM and UM drivers Bottom-most I/O
- 146. UMDF OSR USB Sample Walk-through I/O Target usage in the sample filter (src\umdf\usb\filter) Obtaining default I/O
- 147. Request Cancellation
- 148. Request Cancellation Application can cancel a request at any time by Using CancelIo[Ex] API Exiting application,
- 149. Cancellation Overview Request Owner can register a cancel callback Cancel callback follows locking constraints On Cancel:
- 150. Request Ownership Request ownership starts with I/O Queue Request ownership moves from: Queue to Driver –
- 151. Cancellation Handling in Driver When driver receives a Request, it may: Complete it soon Shortly send
- 152. Cancellation Handling in Driver (con’t) Driver may register a cancel notification callback void IWDFIoRequest::MarkCancelable( IRequestCallbackCancel* pCancelCallback
- 153. Cancellation Illustrated Let us take the example of OSR USB Driver This driver uses device level
- 154. OnRead* OnRead* ctx->state = RequestCompleted; DeviceLock->ReleaseLock(); } Cancellation Illustrated (1) … WinUSB_ReadPipe … Req->MarkCancelable … …
- 155. OnCancel* OnCancel* OnRead* } Cancellation Illustrated (2a) … WinUSB_ReadPipe … Req->MarkCancelable … … OnUSBReadComplete if (Req->UnmarkCancelable())
- 156. OnCancel* OnCancel* } Cancellation Illustrated (2b) … WinUSB_ReadPipe … Req->MarkCancelable … OnRead* … OnUSBReadComplete if (Req->UnmarkCancelable())
- 157. UMDF OSR USB Sample Walk-through Cancellation walk-through in the sample driver (src\umdf\usb\driver) (Queue.cpp) CMyQueue::OnRead CMyQueue::OnCancel CMyQueue::CompletionThread
- 158. Putting It Together You’ve seen the following portions of the sample: Driver (Driver.cpp) CMyDriver::OnDeviceAdd Device (Device.cpp)
- 159. Call to Action Install the Windows Driver Kit Join the WDF Beta Program At http://beta.microsoft.com Guest
- 160. Additional Resources Email umdffdbk @ microsoft.com Web Resources: WDF Information: http://www.microsoft.com/whdc/driver/wdf/default.mspx Windows Debugger: http://www.microsoft.com/whdc/devtools/debugging/default.mspx External Resources
- 162. Скачать презентацию